是否可以使用Mongoose在MongoDB中创建新数据库? [英] Is it possible to create a new database in MongoDB with Mongoose?

查看:74
本文介绍了是否可以使用Mongoose在MongoDB中创建新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定是否可以使用Mongoose在MongoDB中创建新数据库.我在Node上运行,并且我知道Node的MongoDB驱动程序可以做到这一点,但是我想知道是否可以仅从Mongoose做到这一点.

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 I can do it just from Mongoose.

Mongoose中的Node MongoDB驱动程序中的db.createCollection(name, options)是否等效?我的Google技术现在失败了.

Is there an equivalent to the db.createCollection(name, options) from the Node MongoDB driver in Mongoose? My google skills are failing right now.

我只是想弄清楚是否必须为此安装整个MongoDB驱动程序,但我认为是.

I just was trying to figure out if I had to install the whole MongoDB driver just for that, but I think I do.

推荐答案

是的,您可以在连接字符串中指定数据库名称.

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

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

一旦创建了具有该连接的记录,它将在数据库名称"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')

,这将创建所有名为'dbname2'的记录.您的文档不会导入到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.

这篇关于是否可以使用Mongoose在MongoDB中创建新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆