Is it possible to create a new database in MongoDB with Mongoose?

前端 未结 2 1964
北海茫月
北海茫月 2021-02-13 19:00

I am trying to figure out if I can create a new database in MongoDB with Mongoose. I am running on Node, and I know the MongoDB driver for Node can do it, but I am wondering if

2条回答
  •  伪装坚强ぢ
    2021-02-13 19:57

    Yes, you can specify the database name in your connection string.

    db = mongoose.connect('mongodb://localhost/dbname1')
    

    As soon as you create a record with that connection, it will create the new database and collections under the database name of 'dbname1'. If you wanted to create a new database, you can specify a different connection string:

    db = mongoose.connect('mongodb://localhost/dbname2')
    

    and that will create all your records under the name 'dbname2'. Your documents will not import over to dbname2, you will have to do an import of those records if you wanted to do that. Hope that helps.

提交回复
热议问题