MongoDB not creating database in shell

后端 未结 2 1488
陌清茗
陌清茗 2020-12-07 01:49

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

相关标签:
2条回答
  • 2020-12-07 02:20

    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.

    0 讨论(0)
  • 2020-12-07 02:40

    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
    
    0 讨论(0)
提交回复
热议问题