如何获取文档数组中元素的计数-MongoDB
How to get count of elements in document array - MongoDB?
我有以下MongoDB集合(JSON):
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "test@gmail.com",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156999",
"email" : "a@gmail.com",
},
{
"id" : "570724e4ae4f8a5026156333",
"email" : "a2@gmail.com",
},
{
"id" : "570724e4ae4f8a5026156111",
"email" : "a3@gmail.com",
},
{
"id" : "570724e4ae4f8a5026156222",
"email" : "a4@gmail.com",
}
],
},
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "test@gmail.com",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156555",
"email" : "a@gmail.com",
},
{
"id" : "570724e4ae4f8a5026156666",
"email" : "a2@gmail.com",
},
],
},
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "test2@gmail.com",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156555",
"email" : "a@gmail.com",
},
{
"id" : "570724e4ae4f8a5026156666",
"email" : "a2@gmail.com",
},
],
}
我想获得文档的所有数组中元素的计数,其中电子邮件=test@gmail.com.我怎样才能开始计数呢?
我正在使用以下内容获取带有电子邮件的文档数量test@gmail.com使用这个:
collection.count({"email" : tmpEmail}, function (err, count) {
res.json(count);
console.log("Number: " + count);
});
我如何继续计算电子邮件所在文档的所有申请人数组中的元素数量test@gmail.com?对于上面的例子,可能是:6。
编辑:
根据其中一个答案,我将查询修改为以下内容:
答案1:
collection.aggregate(
{$match: {"email": req.user.username, "status" : "true"}},
{$unwind: "$applicants"},
{$group: {_id:null, count: {$sum :1}}, function (err, count) {
res.json(count);
console.log("Number of New Applicants: " + count);
}
});
答案2:
collection.aggregate(
[{$match:{"email" : req.user.username, "status" : "true"}},
{$project:{_id:0, email:1, totalApplicants:{$size:"$applicants"}}},
{$group:{_id:"$employer", count:{$sum:"$totalApplicants"}}}],
function (err, count){
res.json(count);
console.log("Number of New Applicants: " + count);
});
您可以使用聚合查询:
collection.aggregate(
[{$match: {"email": req.user.username, "status" : "true"}},
{$unwind: "$applicants"},
{$group: {_id:null, count: {$sum :1}}}], function (err, result) {
console.log(result);
console.log("Number of New Applicants: " + result[0].count);
if(result.length > 0)
res.json(result[0]);
else
res.json({count:0});
}
});
这将导致您在一个文档中,count将具有您所需的结果
这可能需要编写聚合,因为您需要计算通过电子邮件分组的申请者数组的大小:
以下是等效的mongodb查询,它返回计数为的预期电子邮件
db.yourCollection.aggregate(
[{$match:{"email" : "test@gmail.com"}},
{$project:{_id:0, email:1,totalEmails:{$size:"$applicants"}}},
{$group:{_id:"$email", count:{$sum:"$totalEmails"}}}])
这将返回{ "_id" : "test@gmail.com", "count" : 6 }
您可能需要根据您的代码对此进行更改。
相关文章:
- 如何获取文档数组中元素的计数-MongoDB
- 如何在MongoDB对象的每个嵌套数组中添加一个额外的元素
- 在MongoDB和Node.js中查找某个字段具有与对象完全相同元素的文档
- MongoDB - 查找元素,其中字段等于数组中的至少一个值
- MongoDB:如何使用特定规则查询$in中的特定元素
- 返回嵌套数组中的第一个元素,使用 mongodb 驱动程序为 node.js
- MongoDB获取最后一个元素不能在node.js上正常工作
- Meteor 无法使用 JQuery 激活元素,因为模板渲染了来自 MongoDB 的数据
- mongodb:如何为每个数组元素创建_id
- Mongo DB,Node Express,从MongoDB返回的输出中提取一个元素
- MongoDB/Meteor:为每个数组元素添加唯一的ID
- 在mongoDb中,如果字符串输入与任何数组元素匹配,如何获取整个记录(在这种情况下,数组是记录的属性)
- Mongoose如何在现有对象(MongoDB和Node.js)上插入元素
- mongodb:以原子方式删除数组的前n个元素
- 流星.js&mongodb-如何在每个空格中传递当前元素的id
- MongoDB:选择2个字段日期相同的所有元素
- 如何删除mongodb数组中的元素
- mongodb只更新特定的子元素
- MongoDB中的Node.js更新元素
- 删除mongodb中的数组元素