使用Mongoose查找和计数集合的元素
在Mongoose中,我需要在集合中查找元素并对它们进行计数,并获得查找和计数的结果。我努力了
Model.find().count(function (err, count) {
// Get count, but cannot get results of find
});
有没有办法获得find()和count()而不调用它们两次?
回答如下:您可以使用返回数组的长度:
Model.find().exec(function (err, results) {
var count = results.length
});
使用Mongoose查找和计数集合的元素
在Mongoose中,我需要在集合中查找元素并对它们进行计数,并获得查找和计数的结果。我努力了
Model.find().count(function (err, count) {
// Get count, but cannot get results of find
});
有没有办法获得find()和count()而不调用它们两次?
回答如下:您可以使用返回数组的长度:
Model.find().exec(function (err, results) {
var count = results.length
});