我如何在mongodb中为$ arrayElemAt使用条件?

时间:2019-02-05 09:29:46

标签: node.js angularjs mongodb

我正在使用$ arrayElemAt计算检查计数,但正在返回所有元素计数。我需要获取具有$ projectdata.inspection_data.published =='Y'

的计数
"inspection_count" : {$size: {"$ifNull":[{$arrayElemAt:["$projectdata.inspection_data",0]},[] ] } }

如何根据条件获取计数

我的代码:

 mongo.folder.aggregate([
                    {
                        $lookup:
                        {
                            from: 'inspections',
                            localField: '_id',
                            foreignField: 'projectID',
                            as: 'projectdata'
                        }        
                    },
                    { $match : {
                        $and: [ 
                             querydata
                        ]
                   }
                },
                    {
                        "$project": {
                            "folder_name" : "$folder_name",
                            "location_name": "$location_name", 
                            "architect_name" :"$architect_name",
                            "cover_img": "$cover_img",
                            "created_date" : "$created_date",
                            "parent_id":"$parent_id",
                            "share" : "$share",
                            "share_id":'$share_id',
                            "user_id": "$user_id",
                            "customer_id" : "$customer_id",
                            "download_access" :  "$download_access",
                            "inspection_count": {
                                "$size": {
                                  "$filter": {
                                    "input": "$projectdata.inspection_data",
                                    "as": "el",
                                    "cond": { "$eq": [ "$$el.published", "Y" ] }
                                  }
                                }
                              }
                            }
                    }
                ]).exec(function (err, response) {
                    console.log("Inside Parent Id 1")

                    console.log(response)

                            if(response){
                            response.splice(0, 0, responseparent);
                            }
                            else
                            {
                            response  = responseparent;
                            }
                    if (err){ 


                         res.json({ 'folder': [], 'filelist': [] }); } else {

                            res.json({ 'folder': response, 'filelist': [] }); }
                });

现在显示:

[ { _id: 00000000d080d32e387121c2,
    folder_name: 'Eq1',
    location_name: 'Chennai',
    architect_name: 'Architect Hafeez',
    cover_img: '1539170072349-rmTeigYTa9pmjpdP.png',
    created_date: 2018-10-24T06:05:33.533Z,
    parent_id: [ 00000000d080d32e387121c0 ],
    share: 'N',
    share_id: [ 00000000d080d32e387121bf, 000000002ef020361c3d1ff7 ],
    user_id: 00000000400376127c1a8a31,
    customer_id: [ 00000000d080d32e387121bf ],
    download_access: 'Y',
    inspection_count: 21 },
 ]

期望:

[ { _id: 00000000d080d32e387121c2,
    folder_name: 'Eq1',
    location_name: 'Chennai',
    architect_name: 'Architect Hafeez',
    cover_img: '1539170072349-rmTeigYTa9pmjpdP.png',
    created_date: 2018-10-24T06:05:33.533Z,
    parent_id: [ 00000000d080d32e387121c0 ],
    share: 'N',
    share_id: [ 00000000d080d32e387121bf, 000000002ef020361c3d1ff7 ],
    user_id: 00000000400376127c1a8a31,
    customer_id: [ 00000000d080d32e387121bf ],
    download_access: 'Y',
    inspection_count: 3 },
 ]

总计21,但我只需要显示哪个发布状态为'Y

0 个答案:

没有答案