本地驱动程序从Mongoose模型中查找不返回游标
我试图通过Mongoose Model
的collection
属性执行本地MongoDB find
查询。 我没有提供callback,所以我期望查找返回一个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
我试图用node-inspector进入代码,但它不让我。 任何想法我做错了什么?
本地驱动程序方法都代理在nextTick上运行,因此不会返回驱动程序的返回值。
相反,你可以传递一个callback,第二个返回值是游标。
User.collection.find({}, function (err, cursor) { // });
好奇你为什么需要绕过mongoose?