Mongoose populate query return all results with skip/limitmongodb/mongoose findMany - find all documents with IDs listed in arrayQuerying after populate in MongoosePopulate nested array in mongooseReturn results mongoose in find query to a variableMongoose sort by populated fieldMongoose: findOneAndUpdate doesn't return updated documentLimit query results after populating in MongoDB (Mongoose)Mongoose date comparison does not return correct resultMongoose pre.remove middleware of objects in array are never calledHow to find documents in mongoose by populate field?

Rotate ASCII Art by 45 Degrees

Why was Sir Cadogan fired?

Can a virus destroy the BIOS of a modern computer?

Can compressed videos be decoded back to their uncompresed original format?

Sums of two squares in arithmetic progressions

Does int main() need a declaration on C++?

Are British MPs missing the point, with these 'Indicative Votes'?

How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?

How could indestructible materials be used in power generation?

What is a Samsaran Word™?

Does marriage to a non-Numenorean disqualify a candidate for the crown of Gondor?

Finitely generated matrix groups whose eigenvalues are all algebraic

What does the same-ish mean?

How to prevent "they're falling in love" trope

Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)

Is this draw by repetition?

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

How can a day be of 24 hours?

Is it a bad idea to plug the other end of ESD strap to wall ground?

Calculate the Mean mean of two numbers

Mathematica command that allows it to read my intentions

In Bayesian inference, why are some terms dropped from the posterior predictive?

How can saying a song's name be a copyright violation?

Is it "common practice in Fourier transform spectroscopy to multiply the measured interferogram by an apodizing function"? If so, why?



Mongoose populate query return all results with skip/limit


mongodb/mongoose findMany - find all documents with IDs listed in arrayQuerying after populate in MongoosePopulate nested array in mongooseReturn results mongoose in find query to a variableMongoose sort by populated fieldMongoose: findOneAndUpdate doesn't return updated documentLimit query results after populating in MongoDB (Mongoose)Mongoose date comparison does not return correct resultMongoose pre.remove middleware of objects in array are never calledHow to find documents in mongoose by populate field?













1















I have the following method in a little node/express app :



async getAll(req, res) 
const movies = await movieModel
.find()
.populate(path: 'genres', select: 'name')
.skip(0)
.limit(15);
return res.send(movies);
;


With the following schema :



const MovieSchema = new mongoose.Schema(
externalId: required: true, type: Number ,
title: required: true, type: String ,
genres: [ ref: "Genre", type: mongoose.Schema.Types.ObjectId ],
releaseDate: type: Date,
originalLanguage: type : String,
originalTitle: type : String,
posterPath: type : String,
backdropPath: type : String,
overview: type: String,
comments: [ ref: "Comment", type: mongoose.Schema.Types.ObjectId ],
votes: [VoteSchema]
, timestamps: true
});

MovieSchema.virtual("averageNote").get(function ()
let avg = 0;
if (this.votes.length == 0)
return '-';

this.votes.forEach(vote =>
avg += vote.note;
);
avg = avg / this.votes.length;
return avg.toFixed(2);
);

MovieSchema.set("toJSON",
transform: (doc, ret) =>
ret.id = ret._id;
delete ret._id;
delete ret.__v;
,
virtuals: true,
getters: true
);


However the query always return all document entries.
I also tried to add exec() at the end of the query or with .populate(path: 'genres', select: 'name', options: skip: 0, limit: 15 ) but without result.



I tried on an other schema which is simpler and skip/limit worked just fine, so issue probably comes from my schema but I can't figure out where the problem is.



I also tried with the virtual field commented but still, limit and sort where not used.



My guess is that it's comes from votes: [VoteSchema] since it's the first time I use this, but it was recommanded by my teacher as using ref
isn't recommended in a non relational database. Furthermore, in order to calculate the averageNote as a virtual field, I have no other choice.



EDIT : just tried it back with votes: [ ref: "Vote", type: mongoose.Schema.Types.ObjectId ] And I still can't limit nor skip



Node version : 10.15.1



MongoDB version : 4.0.6



Mongoose version : 5.3.1



Let me know if I should add any other informations










share|improve this question
























  • What are you only trying to return 15 of? Is it the Movies? Or is it the Genres within the movies?

    – Neil Lunn
    Mar 7 at 21:05











  • I am only trying to return 15 Movies, usually movies have at max 4 Genres, but you are rigth, I misunderstood the option in the populate

    – L. Faros
    Mar 7 at 21:07












  • You would not be alone in misunderstanding populate.Presumably the limit(15) just works if the populate() is not there at all. Try that just to test, and then try .find().limit(15).populate('genres', 'name').exec() which I suspect due to the way mongoose internals slightly mangle things would be okay.

    – Neil Lunn
    Mar 7 at 21:13











  • Indeed, I just tried and it works. Thank you very much sir. If you mind writing an answer I'll accept it. Also I didn't know about the populate syntax you mentionned, is it the equivalent of passing an object with path and select or somehow different ? I am also curious as to why the exec() must be used but I might have to read the docs more thoroughly

    – L. Faros
    Mar 7 at 21:21
















1















I have the following method in a little node/express app :



async getAll(req, res) 
const movies = await movieModel
.find()
.populate(path: 'genres', select: 'name')
.skip(0)
.limit(15);
return res.send(movies);
;


With the following schema :



const MovieSchema = new mongoose.Schema(
externalId: required: true, type: Number ,
title: required: true, type: String ,
genres: [ ref: "Genre", type: mongoose.Schema.Types.ObjectId ],
releaseDate: type: Date,
originalLanguage: type : String,
originalTitle: type : String,
posterPath: type : String,
backdropPath: type : String,
overview: type: String,
comments: [ ref: "Comment", type: mongoose.Schema.Types.ObjectId ],
votes: [VoteSchema]
, timestamps: true
});

MovieSchema.virtual("averageNote").get(function ()
let avg = 0;
if (this.votes.length == 0)
return '-';

this.votes.forEach(vote =>
avg += vote.note;
);
avg = avg / this.votes.length;
return avg.toFixed(2);
);

MovieSchema.set("toJSON",
transform: (doc, ret) =>
ret.id = ret._id;
delete ret._id;
delete ret.__v;
,
virtuals: true,
getters: true
);


However the query always return all document entries.
I also tried to add exec() at the end of the query or with .populate(path: 'genres', select: 'name', options: skip: 0, limit: 15 ) but without result.



I tried on an other schema which is simpler and skip/limit worked just fine, so issue probably comes from my schema but I can't figure out where the problem is.



I also tried with the virtual field commented but still, limit and sort where not used.



My guess is that it's comes from votes: [VoteSchema] since it's the first time I use this, but it was recommanded by my teacher as using ref
isn't recommended in a non relational database. Furthermore, in order to calculate the averageNote as a virtual field, I have no other choice.



EDIT : just tried it back with votes: [ ref: "Vote", type: mongoose.Schema.Types.ObjectId ] And I still can't limit nor skip



Node version : 10.15.1



MongoDB version : 4.0.6



Mongoose version : 5.3.1



Let me know if I should add any other informations










share|improve this question
























  • What are you only trying to return 15 of? Is it the Movies? Or is it the Genres within the movies?

    – Neil Lunn
    Mar 7 at 21:05











  • I am only trying to return 15 Movies, usually movies have at max 4 Genres, but you are rigth, I misunderstood the option in the populate

    – L. Faros
    Mar 7 at 21:07












  • You would not be alone in misunderstanding populate.Presumably the limit(15) just works if the populate() is not there at all. Try that just to test, and then try .find().limit(15).populate('genres', 'name').exec() which I suspect due to the way mongoose internals slightly mangle things would be okay.

    – Neil Lunn
    Mar 7 at 21:13











  • Indeed, I just tried and it works. Thank you very much sir. If you mind writing an answer I'll accept it. Also I didn't know about the populate syntax you mentionned, is it the equivalent of passing an object with path and select or somehow different ? I am also curious as to why the exec() must be used but I might have to read the docs more thoroughly

    – L. Faros
    Mar 7 at 21:21














1












1








1








I have the following method in a little node/express app :



async getAll(req, res) 
const movies = await movieModel
.find()
.populate(path: 'genres', select: 'name')
.skip(0)
.limit(15);
return res.send(movies);
;


With the following schema :



const MovieSchema = new mongoose.Schema(
externalId: required: true, type: Number ,
title: required: true, type: String ,
genres: [ ref: "Genre", type: mongoose.Schema.Types.ObjectId ],
releaseDate: type: Date,
originalLanguage: type : String,
originalTitle: type : String,
posterPath: type : String,
backdropPath: type : String,
overview: type: String,
comments: [ ref: "Comment", type: mongoose.Schema.Types.ObjectId ],
votes: [VoteSchema]
, timestamps: true
});

MovieSchema.virtual("averageNote").get(function ()
let avg = 0;
if (this.votes.length == 0)
return '-';

this.votes.forEach(vote =>
avg += vote.note;
);
avg = avg / this.votes.length;
return avg.toFixed(2);
);

MovieSchema.set("toJSON",
transform: (doc, ret) =>
ret.id = ret._id;
delete ret._id;
delete ret.__v;
,
virtuals: true,
getters: true
);


However the query always return all document entries.
I also tried to add exec() at the end of the query or with .populate(path: 'genres', select: 'name', options: skip: 0, limit: 15 ) but without result.



I tried on an other schema which is simpler and skip/limit worked just fine, so issue probably comes from my schema but I can't figure out where the problem is.



I also tried with the virtual field commented but still, limit and sort where not used.



My guess is that it's comes from votes: [VoteSchema] since it's the first time I use this, but it was recommanded by my teacher as using ref
isn't recommended in a non relational database. Furthermore, in order to calculate the averageNote as a virtual field, I have no other choice.



EDIT : just tried it back with votes: [ ref: "Vote", type: mongoose.Schema.Types.ObjectId ] And I still can't limit nor skip



Node version : 10.15.1



MongoDB version : 4.0.6



Mongoose version : 5.3.1



Let me know if I should add any other informations










share|improve this question
















I have the following method in a little node/express app :



async getAll(req, res) 
const movies = await movieModel
.find()
.populate(path: 'genres', select: 'name')
.skip(0)
.limit(15);
return res.send(movies);
;


With the following schema :



const MovieSchema = new mongoose.Schema(
externalId: required: true, type: Number ,
title: required: true, type: String ,
genres: [ ref: "Genre", type: mongoose.Schema.Types.ObjectId ],
releaseDate: type: Date,
originalLanguage: type : String,
originalTitle: type : String,
posterPath: type : String,
backdropPath: type : String,
overview: type: String,
comments: [ ref: "Comment", type: mongoose.Schema.Types.ObjectId ],
votes: [VoteSchema]
, timestamps: true
});

MovieSchema.virtual("averageNote").get(function ()
let avg = 0;
if (this.votes.length == 0)
return '-';

this.votes.forEach(vote =>
avg += vote.note;
);
avg = avg / this.votes.length;
return avg.toFixed(2);
);

MovieSchema.set("toJSON",
transform: (doc, ret) =>
ret.id = ret._id;
delete ret._id;
delete ret.__v;
,
virtuals: true,
getters: true
);


However the query always return all document entries.
I also tried to add exec() at the end of the query or with .populate(path: 'genres', select: 'name', options: skip: 0, limit: 15 ) but without result.



I tried on an other schema which is simpler and skip/limit worked just fine, so issue probably comes from my schema but I can't figure out where the problem is.



I also tried with the virtual field commented but still, limit and sort where not used.



My guess is that it's comes from votes: [VoteSchema] since it's the first time I use this, but it was recommanded by my teacher as using ref
isn't recommended in a non relational database. Furthermore, in order to calculate the averageNote as a virtual field, I have no other choice.



EDIT : just tried it back with votes: [ ref: "Vote", type: mongoose.Schema.Types.ObjectId ] And I still can't limit nor skip



Node version : 10.15.1



MongoDB version : 4.0.6



Mongoose version : 5.3.1



Let me know if I should add any other informations







node.js mongodb mongoose mongodb-query






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 21:31









Neil Lunn

101k23178187




101k23178187










asked Mar 7 at 20:55









L. FarosL. Faros

192113




192113












  • What are you only trying to return 15 of? Is it the Movies? Or is it the Genres within the movies?

    – Neil Lunn
    Mar 7 at 21:05











  • I am only trying to return 15 Movies, usually movies have at max 4 Genres, but you are rigth, I misunderstood the option in the populate

    – L. Faros
    Mar 7 at 21:07












  • You would not be alone in misunderstanding populate.Presumably the limit(15) just works if the populate() is not there at all. Try that just to test, and then try .find().limit(15).populate('genres', 'name').exec() which I suspect due to the way mongoose internals slightly mangle things would be okay.

    – Neil Lunn
    Mar 7 at 21:13











  • Indeed, I just tried and it works. Thank you very much sir. If you mind writing an answer I'll accept it. Also I didn't know about the populate syntax you mentionned, is it the equivalent of passing an object with path and select or somehow different ? I am also curious as to why the exec() must be used but I might have to read the docs more thoroughly

    – L. Faros
    Mar 7 at 21:21


















  • What are you only trying to return 15 of? Is it the Movies? Or is it the Genres within the movies?

    – Neil Lunn
    Mar 7 at 21:05











  • I am only trying to return 15 Movies, usually movies have at max 4 Genres, but you are rigth, I misunderstood the option in the populate

    – L. Faros
    Mar 7 at 21:07












  • You would not be alone in misunderstanding populate.Presumably the limit(15) just works if the populate() is not there at all. Try that just to test, and then try .find().limit(15).populate('genres', 'name').exec() which I suspect due to the way mongoose internals slightly mangle things would be okay.

    – Neil Lunn
    Mar 7 at 21:13











  • Indeed, I just tried and it works. Thank you very much sir. If you mind writing an answer I'll accept it. Also I didn't know about the populate syntax you mentionned, is it the equivalent of passing an object with path and select or somehow different ? I am also curious as to why the exec() must be used but I might have to read the docs more thoroughly

    – L. Faros
    Mar 7 at 21:21

















What are you only trying to return 15 of? Is it the Movies? Or is it the Genres within the movies?

– Neil Lunn
Mar 7 at 21:05





What are you only trying to return 15 of? Is it the Movies? Or is it the Genres within the movies?

– Neil Lunn
Mar 7 at 21:05













I am only trying to return 15 Movies, usually movies have at max 4 Genres, but you are rigth, I misunderstood the option in the populate

– L. Faros
Mar 7 at 21:07






I am only trying to return 15 Movies, usually movies have at max 4 Genres, but you are rigth, I misunderstood the option in the populate

– L. Faros
Mar 7 at 21:07














You would not be alone in misunderstanding populate.Presumably the limit(15) just works if the populate() is not there at all. Try that just to test, and then try .find().limit(15).populate('genres', 'name').exec() which I suspect due to the way mongoose internals slightly mangle things would be okay.

– Neil Lunn
Mar 7 at 21:13





You would not be alone in misunderstanding populate.Presumably the limit(15) just works if the populate() is not there at all. Try that just to test, and then try .find().limit(15).populate('genres', 'name').exec() which I suspect due to the way mongoose internals slightly mangle things would be okay.

– Neil Lunn
Mar 7 at 21:13













Indeed, I just tried and it works. Thank you very much sir. If you mind writing an answer I'll accept it. Also I didn't know about the populate syntax you mentionned, is it the equivalent of passing an object with path and select or somehow different ? I am also curious as to why the exec() must be used but I might have to read the docs more thoroughly

– L. Faros
Mar 7 at 21:21






Indeed, I just tried and it works. Thank you very much sir. If you mind writing an answer I'll accept it. Also I didn't know about the populate syntax you mentionned, is it the equivalent of passing an object with path and select or somehow different ? I am also curious as to why the exec() must be used but I might have to read the docs more thoroughly

– L. Faros
Mar 7 at 21:21













1 Answer
1






active

oldest

votes


















1














This is actually more about how .populate() actually works and why the order of "chained methods" here is important. But in brief:



const movies = await movieModel
.find()
.skip(0)
.limit(15)
.populate(path: 'genres', select: 'name') // alternately .populate('genres','name')
.exec()


The problem is that .populate() really just runs another query to the database to "emulate" a join. This is not really anything to do with the original .find() since all populate() does is takes the results from the query and uses certain values to "look up" documents in another collection, using that other query. Importantly the results come last.



The .skip() and .limit() on the other had are cursor modifiers and directly part of the underlying MongoDB driver. These belong to the .find() and as such these need to be in sequence



The MongoDB driver part of the builder is is forgiving in that:



.find().limit(15).skip(0)


is also acceptable due to the way the options pass in "all at once", however it's good practice to think of it as skip then limit in that order.



Overall, the populate() method must be the last thing on the chain after any cursor modifiers such as limit() or skip().






share|improve this answer























  • Thank you, I could have lost hours trying to figure that out by myself. Just one more question, why is the .exec() method used ? I just tried without it and the result is the same.

    – L. Faros
    Mar 7 at 21:55











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052648%2fmongoose-populate-query-return-all-results-with-skip-limit%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is actually more about how .populate() actually works and why the order of "chained methods" here is important. But in brief:



const movies = await movieModel
.find()
.skip(0)
.limit(15)
.populate(path: 'genres', select: 'name') // alternately .populate('genres','name')
.exec()


The problem is that .populate() really just runs another query to the database to "emulate" a join. This is not really anything to do with the original .find() since all populate() does is takes the results from the query and uses certain values to "look up" documents in another collection, using that other query. Importantly the results come last.



The .skip() and .limit() on the other had are cursor modifiers and directly part of the underlying MongoDB driver. These belong to the .find() and as such these need to be in sequence



The MongoDB driver part of the builder is is forgiving in that:



.find().limit(15).skip(0)


is also acceptable due to the way the options pass in "all at once", however it's good practice to think of it as skip then limit in that order.



Overall, the populate() method must be the last thing on the chain after any cursor modifiers such as limit() or skip().






share|improve this answer























  • Thank you, I could have lost hours trying to figure that out by myself. Just one more question, why is the .exec() method used ? I just tried without it and the result is the same.

    – L. Faros
    Mar 7 at 21:55















1














This is actually more about how .populate() actually works and why the order of "chained methods" here is important. But in brief:



const movies = await movieModel
.find()
.skip(0)
.limit(15)
.populate(path: 'genres', select: 'name') // alternately .populate('genres','name')
.exec()


The problem is that .populate() really just runs another query to the database to "emulate" a join. This is not really anything to do with the original .find() since all populate() does is takes the results from the query and uses certain values to "look up" documents in another collection, using that other query. Importantly the results come last.



The .skip() and .limit() on the other had are cursor modifiers and directly part of the underlying MongoDB driver. These belong to the .find() and as such these need to be in sequence



The MongoDB driver part of the builder is is forgiving in that:



.find().limit(15).skip(0)


is also acceptable due to the way the options pass in "all at once", however it's good practice to think of it as skip then limit in that order.



Overall, the populate() method must be the last thing on the chain after any cursor modifiers such as limit() or skip().






share|improve this answer























  • Thank you, I could have lost hours trying to figure that out by myself. Just one more question, why is the .exec() method used ? I just tried without it and the result is the same.

    – L. Faros
    Mar 7 at 21:55













1












1








1







This is actually more about how .populate() actually works and why the order of "chained methods" here is important. But in brief:



const movies = await movieModel
.find()
.skip(0)
.limit(15)
.populate(path: 'genres', select: 'name') // alternately .populate('genres','name')
.exec()


The problem is that .populate() really just runs another query to the database to "emulate" a join. This is not really anything to do with the original .find() since all populate() does is takes the results from the query and uses certain values to "look up" documents in another collection, using that other query. Importantly the results come last.



The .skip() and .limit() on the other had are cursor modifiers and directly part of the underlying MongoDB driver. These belong to the .find() and as such these need to be in sequence



The MongoDB driver part of the builder is is forgiving in that:



.find().limit(15).skip(0)


is also acceptable due to the way the options pass in "all at once", however it's good practice to think of it as skip then limit in that order.



Overall, the populate() method must be the last thing on the chain after any cursor modifiers such as limit() or skip().






share|improve this answer













This is actually more about how .populate() actually works and why the order of "chained methods" here is important. But in brief:



const movies = await movieModel
.find()
.skip(0)
.limit(15)
.populate(path: 'genres', select: 'name') // alternately .populate('genres','name')
.exec()


The problem is that .populate() really just runs another query to the database to "emulate" a join. This is not really anything to do with the original .find() since all populate() does is takes the results from the query and uses certain values to "look up" documents in another collection, using that other query. Importantly the results come last.



The .skip() and .limit() on the other had are cursor modifiers and directly part of the underlying MongoDB driver. These belong to the .find() and as such these need to be in sequence



The MongoDB driver part of the builder is is forgiving in that:



.find().limit(15).skip(0)


is also acceptable due to the way the options pass in "all at once", however it's good practice to think of it as skip then limit in that order.



Overall, the populate() method must be the last thing on the chain after any cursor modifiers such as limit() or skip().







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 21:30









Neil LunnNeil Lunn

101k23178187




101k23178187












  • Thank you, I could have lost hours trying to figure that out by myself. Just one more question, why is the .exec() method used ? I just tried without it and the result is the same.

    – L. Faros
    Mar 7 at 21:55

















  • Thank you, I could have lost hours trying to figure that out by myself. Just one more question, why is the .exec() method used ? I just tried without it and the result is the same.

    – L. Faros
    Mar 7 at 21:55
















Thank you, I could have lost hours trying to figure that out by myself. Just one more question, why is the .exec() method used ? I just tried without it and the result is the same.

– L. Faros
Mar 7 at 21:55





Thank you, I could have lost hours trying to figure that out by myself. Just one more question, why is the .exec() method used ? I just tried without it and the result is the same.

– L. Faros
Mar 7 at 21:55



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052648%2fmongoose-populate-query-return-all-results-with-skip-limit%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко