如何计算文档数组中的元素数 - MongoDB?

时间:2016-05-06 02:55:28

标签: javascript node.js mongodb mongoose

我有以下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",
            },
    ],
 }

我想获得文件的所有数组中元素的计数,其中email = 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);
         });

2 个答案:

答案 0 :(得分:3)

您可以改为使用聚合查询:

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});
        }
    });

这将使您获得一个文件,其中计数将具有您所需的结果

答案 1 :(得分:2)

这可能需要编写聚合,因为您需要计算通过电子邮件分组的申请人数组的大小:

这是等效的mongodb查询,它返回带有count的预期电子邮件:

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 }

您可能需要根据您的代码进行更改。