admin管理员组文章数量:1391955
I have the following MongoDB collection (JSON):
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "[email protected]",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156999",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156333",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156111",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156222",
"email" : "[email protected]",
}
],
},
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "[email protected]",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156555",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156666",
"email" : "[email protected]",
},
],
},
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "[email protected]",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156555",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156666",
"email" : "[email protected]",
},
],
}
I would like to get the count of the elements in all arrays of the of the document where the email = [email protected]. How can I go about getting that count?
I am using the following to get the number of documents with email [email protected] using this:
collection.count({"email" : tmpEmail}, function (err, count) {
res.json(count);
console.log("Number: " + count);
});
How can I go ahead and count the number of elements in all applicant arrays for the documents where the email is [email protected]? The could for the example above would be: 6.
EDIT:
As per one of the answers I modified my query to the following:
Answer 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);
}
});
Answer 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);
});
I have the following MongoDB collection (JSON):
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "[email protected]",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156999",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156333",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156111",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156222",
"email" : "[email protected]",
}
],
},
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "[email protected]",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156555",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156666",
"email" : "[email protected]",
},
],
},
{
"_id" : ObjectId("570185458351bbac27bc9a20"),
"email" : "[email protected]",
"applicants" : [
{
"id" : "570724e4ae4f8a5026156555",
"email" : "[email protected]",
},
{
"id" : "570724e4ae4f8a5026156666",
"email" : "[email protected]",
},
],
}
I would like to get the count of the elements in all arrays of the of the document where the email = [email protected]. How can I go about getting that count?
I am using the following to get the number of documents with email [email protected] using this:
collection.count({"email" : tmpEmail}, function (err, count) {
res.json(count);
console.log("Number: " + count);
});
How can I go ahead and count the number of elements in all applicant arrays for the documents where the email is [email protected]? The could for the example above would be: 6.
EDIT:
As per one of the answers I modified my query to the following:
Answer 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);
}
});
Answer 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);
});
Share
Improve this question
edited May 7, 2016 at 1:45
user2573690
asked May 6, 2016 at 2:55
user2573690user2573690
6,0839 gold badges45 silver badges64 bronze badges
2 Answers
Reset to default 4You can use an aggregate query instead:
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});
}
});
This will result you in one document where count will have your required result
This may require to write a aggregation since you need to count the size of applicants array grouped by email:
Here is the equivalent mongodb query that returns the expected email with count:
db.yourCollection.aggregate(
[{$match:{"email" : "[email protected]"}},
{$project:{_id:0, email:1,totalEmails:{$size:"$applicants"}}},
{$group:{_id:"$email", count:{$sum:"$totalEmails"}}}])
This returns { "_id" : "[email protected]", "count" : 6 }
You may need to change this according to your code.
本文标签: javascriptHow to get count of elements in document arrayMongoDBStack Overflow
版权声明:本文标题:javascript - How to get count of elements in document array - MongoDB? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744768964a2624219.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论