5

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
4
  • Default directory for mongodb to store collections is /data/db do you have that folder ? Commented Sep 16, 2015 at 21:27
  • 1
    you need to be in the mongo shell $ mongo and then > use mays within it Commented Sep 16, 2015 at 21:31
  • Add that as the answer please :) /var/lib/mongodb this is my db path Commented Sep 16, 2015 at 21:33
  • @aug2uag I meant you add it as the answer please Commented Sep 16, 2015 at 22:01

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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)

1 Comment

Thanks, although totally illogical not to show up when you just created it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.