[英]Get total count of collection after setting limit and offset using mongodb, mongoose and nodejs
I have seen several questions on this topic on Google but I am not sure why any of them is working for me.我在 Google 上看到了关于这个主题的几个问题,但我不确定为什么其中任何一个对我有用。
I am simply trying to query a data from collection with limit and offset passing which seems working fine but I need one more parameter where it returns the total no.我只是想通过限制和偏移传递从集合中查询数据,这似乎工作正常,但我需要一个返回总数的参数。 of data available in that collection.该集合中可用的数据。
Is it that hard?有那么难吗? I have spend over 12 hours just for this.我为此花费了超过 12 个小时。 Must be I am a beginner in Nodejs and mongodb.一定是我是 Nodejs 和 mongodb 的初学者。
With my research I found that db.collection(collection_name).count())
通过我的研究,我发现db.collection(collection_name).count())
should return total count.应该返回总计数。 But it shows blank object where it is placed.但它显示空白 object 放置它。 I again came to know that it is being depreciated and now we should use Collection.countDocuments or Collection.estimatedDocumentCount
.我再次知道它正在折旧,现在我们应该使用Collection.countDocuments or Collection.estimatedDocumentCount
。 Again check the documents of mongodb .再次检查mongodb 的文件。 As per the document it says: db.orders.countDocuments({})
where I replaced the orders with my collection name which becomes.根据它说的文件: db.orders.countDocuments({})
我用我的集合名称替换了订单。 db.allMinisters.countDocuments({})
I get this error {"error":{"message":"Cannot read property 'countDocuments' of undefined"}}
.我收到此错误{"error":{"message":"Cannot read property 'countDocuments' of undefined"}}
。 I even use db.collection('allMinisters').countDocuments({})
but it return promise and display blank object as count.我什至使用db.collection('allMinisters').countDocuments({})
但它返回 promise 并显示空白 object 作为计数。
My code:-我的代码:-
mongoose.connect( url, (err, database) => {
if(err) {
return console.error(err);
}
db = database;
});
exports.getAllMinisters = (req, res, next)=>{
console.log(db.collection('allMinisters').countDocuments({}));
db.collection('allMinisters').find({}).skip(0).limit(10).toArray(function (err, docs) {
if (err) {return next(err);}
const response = {
count: db.collection('allMinisters').count(),
allMinisters: docs.map(docs=>docs),
status: 'success'
}
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(response));
});
};
How do I get total count?我如何获得总计数?
I am not sure how your REST API looks like but try something like this我不确定你的 REST API 看起来如何,但试试这样
exports.getallMinisters = async (req, res, next) => {
if (req.params.docs) {
const allMinisters = await allMinister.find({ docs: req.params.docs });
return res.status(200).json({
success: true,
count: allMinisters.length,
data: allMinisters
});
} else {
//errorResponse;
}
};
Thanks to @ashok.感谢@ashok。
I manage to fixed this by simply creating async based function and await the total count query.我通过简单地创建基于异步的 function 并等待总计数查询来解决此问题。
exports.getAllMinisters = async (req, res, next)=>{
totalCount = await db.collection('allMinisters').countDocuments({});
db.collection('allMinisters').find({}).skip(0).limit(10).toArray(function (err, docs) {
if (err) {return next(err);}
const response = {
count: totalCount,
allMinisters: docs.map(docs=>docs),
status: 'success'
}
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(response));
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.