E
E
Egor2014-06-22 15:31:36
MongoDB
Egor, 2014-06-22 15:31:36

How to set populate limit in mongoose?

Greetings.
I'm using Mongoose;
There is such a scheme

var UserSchema = new Schema({
 login:{type:String,unique:true,required:true},
_works:[{type:Number,ref:'Work'}]
});

To get a list of works ( _works ) the following construction is used
users.findOne({login:login}).populate('_works').exec(callback);

Question. How to limit the number of jobs that will end up in the callback as a result?
There can be a lot of works, so you don’t want to load them all at once. With a large number, they will be loaded by ajax. That is, it is also necessary to implement some kind of offset.
In which direction to dig to implement such functionality?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Haunted, 2014-06-26
@Haunted

Alternatively, use the mongodb aggregation framework to make $unwind ( mongoosejs.com/docs/api.html#aggregate-js ) splitting one document with a large array into many small ones, but with one element. Further in the code, do pagination.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question