[英]How to count records by criteria in nodejs & mongodb
I have the following JSON object in mongodb我在 mongodb 中有以下 JSON 对象
{
"_id" : ObjectId("585e34c50ab3cd228a8d6828"),
"fullname" : "Name1",
"status" : "INACTIVE",
"createdDate" : ISODate("2016-12-24T08:41:41.225Z"),
"updatedDate" : ISODate("2016-12-24T08:41:41.225Z")
},
{
"_id" : ObjectId("525e34c50ab3cd228a8d6828"),
"fullname" : "Name2",
"status" : "ACTIVE",
"createdDate" : ISODate("2016-12-24T08:41:41.225Z"),
"updatedDate" : ISODate("2016-12-24T08:41:41.225Z")
}
and the codes below return all count of all JSON objects.下面的代码返回所有 JSON 对象的所有计数。
db.collection(col).count(function (err, res) {
if (err)
throw err;
db.close();
});
but i need to count only "ACTIVE" status results.但我只需要计算“ACTIVE”状态结果。 How to do that?
怎么做?
how about just:怎么样:
db.collectionName.find({"status": "ACTIVE"}).count()
Im assuming your collection name is called "collectionName"我假设您的收藏名称称为“收藏名称”
try this尝试这个
db.collection(col).find({"status" : "ACTIVE"}).count(function (err, res) {
if (err)
throw err;
console.log(res)
db.close();
});
db.collection.aggregate(
[
{ $match: status: "ACTIVE" },
{ $group: { _id: null, count: { $sum: 1 } } }
]
)
without find()
没有
find()
db.collection('anycollection')
.count( { status : 'ACTIVE' }, (err, count) => {
console.log(count);
});
By the way , count()
is deprecated now so more correct solution would be顺便说一下,
count()
现在已被弃用,因此更正确的解决方案是
db.collection('anycollection)
.countDocuments( { status : 'ACTIVE' }, (err, count) => {
console.log(count);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.