Find native driver from Mongoose model without returning cursor

I am trying to execute my own MongoDB find query using the collection property of a Mongoose Model object. I do not provide a callback, so I expect find to return a Cursor object, but instead return undefined . According to Mongoose docs , the driver used is available through YourModel.collection , and if I switch to the purely find driver code myself, return a Cursor , so I cannot figure out what is happening.

Here is a piece of code that reproduces the problem:

 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 

I tried to enter the code using the node-inspector, but this does not allow me. Any idea what I'm doing wrong?

+6
source share
1 answer

Native driver methods are proxied to run on the next tick, so return values ​​from the driver are not returned.

Instead, you can pass a callback, and the second argument returned is the cursor.

 User.collection.find({}, function (err, cursor) { // }); 

Curious why you need to bypass the mongoose?

+11
source

Source: https://habr.com/ru/post/915709/


All Articles