>我正在尝试通过猫鼬Model
的collection
属性执行本机MongoDB find
查询。 我没有提供回调,所以我希望 find 返回一个Cursor
对象,但它会返回undefined
。 根据 Mongoose 文档,正在使用的驱动程序可以通过 YourModel.collection
访问,如果我切换到纯粹使用本机驱动程序代码,find
确实会返回一个Cursor
,所以我无法弄清楚发生了什么。
下面是重现该问题的代码片段:
var db = mongoose.connect('localhost', 'test');
var userSchema = new Schema({
username: String,
emailAddress: String
});
var User = mongoose.model('user', userSchema);
var cursor = User.collection.find({});
// cursor will be set to undefined
我试图使用节点检查器进入代码,但它不允许我。 知道我做错了什么吗?
本机驱动程序方法都代理在 nextTick 上运行,因此不会返回驱动程序的返回值。
相反,您可以传递回调,返回的第二个 arg 是光标。
User.collection.find({}, function (err, cursor) {
//
});
好奇为什么需要绕过猫鼬?