There is 3 documents in person collection, but I can't find()
有3个文档在person collection,但是找不到()
mongodb shell:
mongodb shell:
> use test;
switched to db test
> db.person.find();
{ "_id" : ObjectId("527f18f7d0ec1e35065763e4"), "name" : "aaa", "sex" : "man", "height" : 180 }
{ "_id" : ObjectId("527f1903d0ec1e35065763e5"), "name" : "bbb", "sex" : "man", "height" : 160 }
{ "_id" : ObjectId("527f190bd0ec1e35065763e6"), "name" : "ccc", "sex" : "woman", "height" : 160 }
my nodejs code:
我nodejs代码:
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://uuu:ppp@localhost:27017/test');
//mongoose.set('debug', true);
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log("connection success!");
mongoose.model('person', mongoose.Schema({ name: String}),'person');
var out = db.model('person');
//console.log(out);
out.find({},function(err, obj) {
console.log(obj);
console.log(1);
});
});
But, the result is : connection success! [] 1
但是,结果是:连接成功![]1
2 个解决方案
#1
3
The issue is that when you use createConnection
, you need to use the .model
method of the created connection, instead of mongoose.model
:
问题是,当您使用createConnection时,您需要使用创建连接的.model方法,而不是mongoose.model:
var db = mongoose.createConnection(...);
// wrong:
mongoose.model('person', mongoose.Schema({ name: String}),'person');
// right:
db.model('person', mongoose.Schema({ name: String}),'person');
// perform query:
db.model('person').find(...);
The reason is that, as far as I understand, a model is 'associated' to a connection. When you use mongoose.model
, the model is associated to the default connection, but you're not using that (you're using a connection that you create explicitly with createConnection
).
原因是,据我所知,一个模型与一个连接是“关联的”。当你使用猫鼬。模型,模型与默认连接相关联,但是您没有使用它(您使用的是使用createConnection显式创建的连接)。
Another way to show this is using modelNames
:
另一种方式是使用模型名:
// wrong:
mongoose.model('person', mongoose.Schema({ name: String}),'person');
console.log('default conn models:', mongoose.modelNames()); // -> [ 'person' ]
console.log('custom conn models:', db.modelNames()); // -> []
// right:
db.model('person', mongoose.Schema({ name: String}),'person');
console.log('default conn models:', mongoose.modelNames()); // -> []
console.log('custom conn models:', db.modelNames()); // -> [ 'person' ]
#2
0
The problem is that you are not putting your find statement inside the callback for connection.
问题是您没有将find语句放入到connection的回调中。
Try to put it here:
试着把它放在这里:
db.once('open', function callback () {
console.log("connection success!");
mongoose.model('person', mongoose.Schema({ name: String}), 'person');
db.model('person').find({name:'aaa'},function(err, obj) {
console.log(obj);
});
});
#1
3
The issue is that when you use createConnection
, you need to use the .model
method of the created connection, instead of mongoose.model
:
问题是,当您使用createConnection时,您需要使用创建连接的.model方法,而不是mongoose.model:
var db = mongoose.createConnection(...);
// wrong:
mongoose.model('person', mongoose.Schema({ name: String}),'person');
// right:
db.model('person', mongoose.Schema({ name: String}),'person');
// perform query:
db.model('person').find(...);
The reason is that, as far as I understand, a model is 'associated' to a connection. When you use mongoose.model
, the model is associated to the default connection, but you're not using that (you're using a connection that you create explicitly with createConnection
).
原因是,据我所知,一个模型与一个连接是“关联的”。当你使用猫鼬。模型,模型与默认连接相关联,但是您没有使用它(您使用的是使用createConnection显式创建的连接)。
Another way to show this is using modelNames
:
另一种方式是使用模型名:
// wrong:
mongoose.model('person', mongoose.Schema({ name: String}),'person');
console.log('default conn models:', mongoose.modelNames()); // -> [ 'person' ]
console.log('custom conn models:', db.modelNames()); // -> []
// right:
db.model('person', mongoose.Schema({ name: String}),'person');
console.log('default conn models:', mongoose.modelNames()); // -> []
console.log('custom conn models:', db.modelNames()); // -> [ 'person' ]
#2
0
The problem is that you are not putting your find statement inside the callback for connection.
问题是您没有将find语句放入到connection的回调中。
Try to put it here:
试着把它放在这里:
db.once('open', function callback () {
console.log("connection success!");
mongoose.model('person', mongoose.Schema({ name: String}), 'person');
db.model('person').find({name:'aaa'},function(err, obj) {
console.log(obj);
});
});