是否有任何方法可以在find操作中获取文档总数以及MongoDB查询中的跳过和限制
MongoClient.connect(Config.dbURI, function (err, db) {
if (!err) {
console.log("We are connected");
console.log(uniqueId)
db.collection(dbName).find({'uniqueId': uniqueId, 'isDeleted': false})
.sort({modifiedDateISO: -1})
.limit(parseInt(limit))
.skip(parseInt(limit * page))
.toArray((errFindChat, dataFindChat) => {
console.log(errFindChat, dataFindChat);
});
});
最佳答案
我假设“ uniqueId”不是主键!
MongoClient.connect(Config.dbURI, function (err, db) {
if (!err) {
console.log("We are connected");
console.log(uniqueId)
db.collection("collname").aggregate(
[
{ "$match": { "uniqueId": uniqueId, 'isDeleted': false} },
{ "$count": "total" },
{ "$sort" : {"modifiedDateISO": -1 },
{ "$limit": parseInt(limit) },
{ "$skip" : parseInt(limit * page) }
]
).toArray((errFindChat, dataFindChat) => {
console.log(errFindChat, dataFindChat);
});
}
});
MongoDB Aggregate Count