这里不是很明白为什么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){
});
你看的怎么样了,我就栽倒这里了,createConnection,虽然可以open,但完全不会查询。修改成第二种方法,就OK了,搞死了,好几个小时浪费了...
@hzbqjltx 我已经知道了,用传对象的形式,如mongoose.connect(‘host:port/db’, {user: ‘root’, pass: ‘@@@@’});
require('mongoose')返回的是一个Mongoose实例
每个Connection对应一个数据库,由Connection#model定义这个数据库的Model
每个Mongoose实例可以连接多个Connection,这些Connection共用由Mongoose#model定义的Model
这个东西自己写一个就行了,需要什么模块。
参考核心模块http - request + ClientRequest 或者 net - connect + Socket
每一个Nodejs模块的作者都是闲的蛋疼?mongoose已经是一个很停滞的模块,尤其是MongoClient的支持上,用别人的模块前,好好掂量掂量自己肯承受多少风险。
### 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.
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