Reputation: 67
How to display all the available database list even it is not used in mongoDB or it doesn't have any collection even it has no users to newly created database?
> show dbs;
STUDENT 0.000GB
TEACHER 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> use newtest
switched to db newtest
> show dbs;
STUDENT 0.000GB
TEACHER 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
Here I have first list the all dbs after creating newtest tried to redisplay all the DBs but not showing here why?
Upvotes: 2
Views: 892
Reputation: 67
Databases are created and exist after inserting the first document into a collection in the database. Databases and collections are created implicitly; however, creating a database or collection object in your client does not create a database or collection. use does not create new database files.
Empty databases are not listed in the output of show dbs. This is partially storage engine dependent. MMAPv1, it's impossible to have an extant but empty database: you cannot create a database except by inserting data. Even if you later drop that data, there is a small amount of internal overhead that remains. The one exception is that before 3.0, show dbs reported the admin database as empty in some cases, but there were no corresponding. WiredTiger does not report databases that do not have data, other storage engines may have different behavior here.
Upvotes: 1
Reputation: 311935
use newtest
doesn't create the db, it just sets the db context for subsequent commands.
You have to write something to the db for it to be created; for example:
> use newtest
switched to db newtest
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.test.insert({a: 1})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
newtest 0.000GB
Upvotes: 1