Using MongoDB and the Mongoose driver for Node.js, I want to limit the amount of code I have to repeat in my API.
Instead of doing something like this:
var limit = req.query.limit || -1;
if(limit === 1){
Model.findOne({})...
}
else{
Model.find({})...
}
it would be better just to do:
var limit = req.query.limit || -1;
Model.find({}).limit(limit);
So I have two questions:
(1) Does using limit(1) have the same effect as findOne() where it is a much more efficient search because the query returns as soon as it find its first match?
(2) Does limit(-1) or limit(0) have the effect have creating no limit to the search? Because this will make it possible to not have repeat code.
limit(1) vs findOne()
limit(-1), limit(0)
http://docs.mongodb.org/manual/reference/method/cursor.limit/