9

I'm using a book to learn MongoDB. It says that in order to create a db in Mongo, I just need to 'use' it.

show dbs
admin 0.000GB
local 0.000GB

When I do, use someDBName

It says switched to db someDBName

But when I do show dbs

I just see admin and local again. I don't see the someDBName database.

1
  • 3
    It will not appear until it has some data. Try inserting something in it and you will see the db in the list Commented Dec 12, 2016 at 12:26

2 Answers 2

8

It will not show until your database is empty. To have your database shown you can use

create collection command to insert collection then it will be shown to you by using

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

Comments

6

Per the MongoDB documentation:

If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in the mongo shell:

use myNewDB
db.myNewCollection1.insertOne( { x: 1 } )

The insertOne() operation creates both the database myNewDB and the collection myNewCollection1 if they do not already exist.

Comments

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.