带有条件的mongodb中的$ size

时间:2019-02-04 10:30:22

标签: node.js mongodb aggregation

我正在使用聚合从两个集合中获取值,一个是文件夹,另一个是检查。我正在获取所有数据,但是正在检查计数为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': [] }); }
            });

此处检查计数应为3,但计数为零。我如何根据其字段值找到数组的大小。 我使用过过滤器,即使其计数为0

0 个答案:

没有答案