如何在mongoose中创建一个新的数据库? [英] How to create a new db in mongoose?

查看:786
本文介绍了如何在mongoose中创建一个新的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在node.js中使用Mongoose,我需要:

If I use Mongoose in node.js I need to do:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myDB');

但是如果我只是安装了mongodb,我现在没有任何DB?在 mongoose.connect('mongodb:// localhost / myDB')之前,如何在node.js中使用mongoose创建一个新的数据库?或者如果没有这样的 myDB 那么它会创建一个新的?或者会抛出没有这样的DB的错误?

But what if I just installed mongodb and I don't have any DB at the moment? How can I create a new DB using mongoose in node.js before doing mongoose.connect('mongodb://localhost/myDB')? Or if there is no such myDB then it will create a new one? or will throw an error that there is no such a DB?

推荐答案

它不应该抛出任何错误。当您在该数据库的任何新集合中插入文档时,将创建集合和数据库

It should not throw any error. The moment you insert a document in any new collection of that DB, the collection and db will be created

这篇关于如何在mongoose中创建一个新的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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