本机驱动程序从Mongoose模型中找不到返回Cursor

Joh*_*yHK 7 mongoose mongodb node.js

我正在尝试find通过collectionMongoose 的属性执行本机MongoDB 查询Model.我没有提供回调,所以我希望find返回一个Cursor对象,但它会返回undefined.根据该猫鼬文档,正在使用的驱动程序是通过访问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
Run Code Online (Sandbox Code Playgroud)

我试图通过node-inspector进入代码,但它并没有让我这么做.知道我做错了什么吗?

aar*_*ann 11

本机驱动程序方法都代理在nextTick上运行,因此不返回驱动程序的返回值.

相反,您可以传递回调,返回的第二个arg是游标.

User.collection.find({}, function (err, cursor) {
  // 
});
Run Code Online (Sandbox Code Playgroud)

好奇为什么你需要绕过猫鼬?