简体   繁体   English

如何获取文档数组中元素的数量-MongoDB?

[英]How to get count of elements in document array - MongoDB?

I have the following MongoDB collection (JSON): 我有以下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",
            },
    ],
 }

I would like to get the count of the elements in all arrays of the of the document where the email = test@gmail.com. 我想获取电子邮件= test@gmail.com的文档的所有数组中元素的数量。 How can I go about getting that count? 我该如何获得该计数?

I am using the following to get the number of documents with email test@gmail.com using this: 我正在使用以下方法通过以下方式通过电子邮件test@gmail.com获取文档数量:

   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 test@gmail.com? 我该如何继续计算电子邮件为test@gmail.com的文档的所有申请人数组中的元素数? The could for the example above would be: 6. 上面的示例可能是:6。

EDIT: 编辑:

As per one of the answers I modified my query to the following: 根据答案之一,我将查询修改为以下内容:

Answer 1: 答案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: 答案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);
         });

You 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: 以下是等效的mongodb查询,该查询返回带有count的预期电子邮件:

db.yourCollection.aggregate(

  [{$match:{"email" : "test@gmail.com"}}, 

  {$project:{_id:0, email:1,totalEmails:{$size:"$applicants"}}},  

  {$group:{_id:"$email", count:{$sum:"$totalEmails"}}}])

This returns { "_id" : "test@gmail.com", "count" : 6 } 这将返回{ "_id" : "test@gmail.com", "count" : 6 }

You may need to change this according to your code. 您可能需要根据您的代码进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM