Count usage of mongoose

problem description

use mongoose in nodejs

const query=myModel.find({name:"xiaoming"});
const total=query.count();
const data=query.skip(20).limit(10);

Why is it wrong to write in this way?
how to write correctly?

May.10,2021

checked the document, should be because you this is two queries, a count query, a skip plus limit query, so your query query is always the first specified count query, so your second result is actually the result returned by count, so it is the number of number type.

< del > although I don't know why you don't write callbacks or Promise, you can get results. That's how I wrote < / del > ~

.

two queries are fine, because your second query is skip plus limit, so there should be no way to get the total number of documents and paging query results without going through two queries.

const query1 = myModel.find({name:'xiaoming'});
const query2 = myModel.find({name:'xiaoming'});
const total = await query1.count().exec(); //namexiaoming
const data = await query2.skip(20).limit(10).exec();
Menu