Background
I have a query in Mongoose that finds a set of objects, and then returns these said objects together with the total number of them:
var itemsPerPage = 4, pageNum = 1;
Survey.find({})
.limit(itemsPerPage)
.skip(itemsPerPage * pageNum)
.then(docs => {
if (docs.length === 0)
console.log("There are no results matching your query.");
else
DocModel.count({})
.then(number => {
console.log(JSON.stringify({number, docs}));
});
})
.catch(console.log);
To achieve this I do:
find
query with params- use
limit
andskip
to obtain results from correct page - if
docs
is not empty, start acount
query - return information
Problem
The problem here, since I need pagination in the query, is that I have to make 2 separate requests to the DB (find
and count
) which can potential be very heavy.
To optimize this, I would like to avoid the count
query, but I don't know how.
Another issue is the nested promises anti pattern, but that is not the issue I would like to focus on right now.
Question
I am open to any suggestions on improving this code you may have!
1 Answer 1
Solution
After reviewing many answers, I believe I have two options:
- Make two queries, like I am doing now,
- Use a pagination plugin for mongoose
My decision was the second one and so I am now using mongoose-paginate.
Hope it helps.
-
\$\begingroup\$ I am still using the 2 query method up to now. I wish there was an auto index for this kind of total count. \$\endgroup\$kabuto178– kabuto1782017年09月21日 00:48:38 +00:00Commented Sep 21, 2017 at 0:48
-
\$\begingroup\$ This lib
mongoose-paginate
does the same 2 queries: run the query first and again with count.promises = { docs: docsQuery.exec(), count: this.count(query).exec() };
\$\endgroup\$jpenna– jpenna2019年05月18日 13:42:22 +00:00Commented May 18, 2019 at 13:42 -
\$\begingroup\$ And
count
is deprecated, by the way. UsecountDocuments
orestimateCountDocuments
if it is a big set. Mongoose count \$\endgroup\$jpenna– jpenna2019年05月19日 04:03:54 +00:00Commented May 19, 2019 at 4:03
Explore related questions
See similar questions with these tags.