Can't create mongodb db - mongodb

According to this documentation using use mays should create a db if it isn't already created:
MongoDB use DATABASE_NAME is used to create database. The command will
create a new database, if it doesn't exist otherwise it will return
the existing database.
So why doesn't mongo use mays work?
root#server88-208-249-95:~# mongo use mays
MongoDB shell version: 2.6.11
connecting to: use
2015-09-16T22:17:20.316+0100 file [mays] doesn't exist
failed to load: mays

The 'use' command doesn't work with mongo command.
You have to open mongo shell and then use the 'use' command.
Open terminal -> enter 'mongo' to get mongo shell ->
use db_name
This will create a DB if it doesn't exists already.
A DB doesn't show up when using 'show dbs' until you create a collection in it.
Use db.createCollection("collection_name") and then use 'show dbs' and you will see your newly created DB.

You need to create at least one collection before it saves the database. Issuing a use will create it, but will not automatically save it. You can create an empty collection with db.createCollection("test"). To verify try the following commands from the mongo shell:
use mays
show dbs (mays will not show up)
db.createCollection("test")
show dbs (mays will show up)

Related

MongoDB database creation issue

I am using MongoDB. All I want to do is add a collection to a database I have created. I verified before I even started that the database existed by running the command "show dbs". However, when I try to add the collection to that database, it says that this database is undefined. Any thoughts or suggestions? Here were the commands I typed in terminal
use myDB
myDB.myCol.insert({"id":"1"})
the response I got from this command is "myDB is not defined" even
though I verified that it exists with the command "show dbs".
Use db instead of the name of the database in your insert command:
db.myCol.insert({"id":"1"})
use MyDB sets which database db refers to.

How do I specify a repair path from within a mongo shell?

How can I specify a repair path for Mongo (v2.2.0) from within the mongo shell?
For example, I could normally run the command:
mongod --repair --repairpath /opt/vol2/data
How could I specify the repair path if I use the following syntax from within the shell:
db.repairDatabase()
The repairDatabase command will be executed on the database that you are currently connected to and by extension at the path that the current database's data resides.
You can define which database to use by using the use [dbname] command from within the mongo shell:
Taken from the docs:
use <db>
Switch current database to <db>. The mongo shell variable
db is set to the current database.
Alternatively, you can specify which database to use when starting the actual shell. For example, if you wanted to connect to a shell on the my_db database that is running on port 27016, you would launch the mongo shell with the following command:
$ mongo my_db
To find out which database you are currently using, you can simply type db in the shell and you'll get the name of the current database.

Error while creating a database in mongodb (not a valid database name in mongo.js)

I am trying to create a database in mongo db using command use db shorten
when i run the mongod instance it outputs this
but when try to run mongo in the separate terminal it gives error creating a database
the data/db permissions are set to 755 and i couldn't find anything related to it
If your db is called 'shorten' you should just type use shorten. It's the extra 'db' in there that's messing you up.

mongo shell unusable, but access from my program is ok, after uninstalling/new install on OSX

all!
After uninstalling current stable (with brew), and installing the 2.4 rc2 (not brew, since brew --devel does not have it, but mainly as described here: http://shiftcommathree.com/articles/how-to-install-mongodb-on-os-x), even a couple of times, and perhaps being too eager to delete everything mongodb-related before the next install, I have ended up with this situation:
The Node-program I have that uses the database and works ok (find, insert and so on). But the problem is that the mongo shell does not work properly, I cannot insert or find documents. For example (now I have 2.4 rc2, but the situation was the same with the current stable):
$ mongo
MongoDB shell version: 2.4.0-rc2
connecting to: test
> show dbs
local 0.078125GB
> use mydb
switched to db mydb
> db.mydb.mycollection.insert = ({title: "O-oh, strange problem"})
{ "title" : "O-oh, strange problem" }
> show collections
> show dbs
local 0.078125GB
mydb (empty)
Then I do a insert via my Node-program and do this:
> show dbs
local 0.078125GB
mydb 0.203125GB
> use mydb
switched to db mydb
> show collections
mycollection
system.indexes
> db.mydb.mycollection.find()
>
Again, in my Node-program, 'find' finds what it should...
Any idea why mongo shell does not work?
Frode
You've got a collection called mydb.mycollection in a mydb database. It is unlikely that's really what you want. When you use a database from the shell, you are then able to reference the database via the db object without repeating the database name.
use mydb
# db is now implicitly the mydb database
# you no longer refer to it by name
db.mycollection.insert({"name" : "stacks overflowing"})
Also note that you were not calling the insert function, your code was attempting to set the function to the value of the new object.

Some beginner's questions about MongoDB

I'm a beginner with MongoDB and I've some questions:
When I'm connected to Mongo, and i execute show dbs I see 2 databases: admin and local. What's their role? Then if I execute an insert command like db.foo.insert({"value":"mongo"}), the test database appears. Why? How can i specify a custom name for a database?
With show dbs I get the databases (somehow like show databases in sql), how can I then list the collections inside a database (I would use show tables in sql)?
When executing a command, the MongoDB tutorial always uses the db object. Is it the main object (a sort of "connection" object) that has to used for executing commands or it's something else?
Thanks!
admin and local contain various settings local to the server, like users who are authenticated to connect. Under beginner usage, you shouldn't need to worry about them at all. By default you connect to a database named test. To connect to a new database, just use databasename from the mongo command line, or mongo databasename from your OS shell.
use [database_name] and then show collections
The db object is your root handle to the currently-selected database on the mongo commmand line. The command line is really just a Javascript command line, and there are various mongodb-specific objects and functions exposed that let you do stuff. Try help() for a full listing.