[讨论] mongoose.createConnection()和mongoose.connect()
发布于 11 年前 作者 coolicer 29438 次浏览 最后一次编辑是 8 年前

这里不是很明白为什么2个的名字要如此相同,新手都有点糊涂。查了一些文档,当require(‘mongoose’)时,就相当于 mongoose.createConnection(),而如果新手用这个mongoose.model,往往是不成功的。因为永远不会连接。

我也是最近在用这个,欢迎大家讨论,有很多不懂的。

两种方式对比:

var mongoose = require('mongoose');

db = mongoose.createConnection('localhost', 'test');
var schema = new mongoose.Schema({ name: String });
var collectionName = 'kittens';
var M = db.model('Kitten', schema, collectionName);
var silence = new M({ name: "Silence"});
silence.save(function(err){
 
});
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
db = mongoose.connection;
db.once('open', function callback () {
  // yay!
});	
var kittySchema = mongoose.Schema({
 name: String
});
var Kitten = mongoose.model('kitten', kittySchema);
var silence = new Kitten({ name: "Silence"});
silence.save(function(err){

});

22 回复

你看的怎么样了,我就栽倒这里了,createConnection,虽然可以open,但完全不会查询。修改成第二种方法,就OK了,搞死了,好几个小时浪费了…

我也好晕啊,还有我的密码里包含@符号咋整啊

@fancyboynet !!!对哦。我也想知道

@hzbqjltx 我已经知道了,用传对象的形式,如mongoose.connect(‘host:port/db’, {user: ‘root’, pass: ‘@@@@’});

require('mongoose')返回的是一个Mongoose实例 每个Connection对应一个数据库,由Connection#model定义这个数据库的Model 每个Mongoose实例可以连接多个Connection,这些Connection共用由Mongoose#model定义的Model

感谢这个问题,让我把一直模糊的的知识弄清了

@ravenwang 我都忘了,已经好久没搞 :)

@ravenwang 问一下,<code>Connection#model</code>中间加一个井号这样的写法是什么意思?

@gdut-zdc 实例方法。Connection.model 就是类方法。

这个东西自己写一个就行了,需要什么模块。

参考核心模块http - request + ClientRequest 或者 net - connect + Socket

@xuanye

每一个Nodejs模块的作者都是闲的蛋疼?mongoose已经是一个很停滞的模块,尤其是MongoClient的支持上,用别人的模块前,好好掂量掂量自己肯承受多少风险。

@tulayang 一般用还是没有什么问题, 如果为此就要重新开头去分析协议从头开始,我不认为是一个简单的工作

还是不清楚这两者的意义在哪里

同样的情况用createConnection,完全操作不了数据库。改用connection就正常

### Connecting to MongoDB

First, we need to define a connection. If your app uses only one database, you should use `mongoose.connect`. If you need to create additional connections, use `mongoose.createConnection`.

Both `connect` and `createConnection` take a `mongodb://` URI, or the parameters `host, database, port, options`.

```js
var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/my_database');

Once connected, the open event is fired on the Connection instance. If you’re using mongoose.connect, the Connection is mongoose.connection. Otherwise, mongoose.createConnection return value is a Connection.

Important! Mongoose buffers all the commands until it’s connected to the database. This means that you don’t have to wait until it connects to MongoDB in order to define models, run queries, etc.

Connecting to MongoDB

First, we need to define a connection. If your app uses only one database, you should use mongoose.connect. If you need to create additional connections, use mongoose.createConnection.

Both connect and createConnection take a mongodb:// URI, or the parameters host, database, port, options.

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/my_database');

Once connected, the open event is fired on the Connection instance. If you’re using mongoose.connect, the Connection is mongoose.connection. Otherwise, mongoose.createConnection return value is a Connection.

Important! Mongoose buffers all the commands until it’s connected to the database. This means that you don’t have to wait until it connects to MongoDB in order to define models, run queries, etc.

怎样定义 db 才可以使用类似 db.user.find({}) 这样查询方式呢?

http://stackoverflow.com/questions/22786374/queries-hang-when-using-mongoose-createconnection-vs-mongoose-connect 这个有比较详细的介绍。如果使用mongoose.connect的话,它连接的是default connection;而如果是db = mongoose.createConnection(xxxx)的话,因为它返回的是一个connection,所以接下来再进行model生成的时候需要使用 db.model 而不是 mongoose.model

@aisin 这个就需要mongoose.createConnect

回到顶部