Nodejs基于对象属性在嵌套数组中选择对象

时间:2018-01-23 14:22:44

标签: arrays node.js mongodb mongoose mongoose-schema

我是nodejs的新手,目前我正在尝试使用nodejs和mongodb(mongoose)实现Web应用程序。

我想根据这些对象的属性选择嵌套数组和父数组中的对象。

简单 - 我需要选择具有有效属性为true的类别和具有有效属性为true的sub_services。 (在一个类别中,不能有sub_services,其有效属性为false,并且不能有具有有效属性的类别对象为false。)

模式

var VendorSchema = new Schema({

  _id: {
        type: Schema.Types.ObjectId,
        ref: "User"
    },


  services: {

        meta_data: {
              allowed_category_count: {
                     type: Number,
                     default: 1
              }
        },

        categories: [{

              _id:false,

              valid :{
                     type: Boolean,
                     default: false
              },

              service_category: {             
                     type: Schema.Types.ObjectId,
                     ref: "ServiceCategory"
              },

              sub_services :[{
                     _id:false,

                     valid :{
                            type: Boolean,
                            default: false
                     },

                     service : {
                            type: Schema.Types.ObjectId,
                            ref: "Service"
                     }                          
              }]       
        }]
  },

});

查询

 router.get('/get_service_details',function(req, res) {
     Vendor.findOne({ _id: req.user._id,'services.categories.valid': true,'services.categories.sub_services.valid': true},'services').
                exec(function (err, story) {
                    if (err) {
                        console.log(err);
                        return (err);
                    }
                    else{
                        res.send(story);
                    }   
                }) 
    });

来自上述查询的结果

{
    "_id": "5a62ea5d7515222464e20016",
    "services": {
        "categories": [
            {
                "service_category": {
                    "_id": "5a609b40c9a5e50d844838bf"
                },
                "sub_services": [
                    {
                        "service": {
                            "_id": "5a609f7ac9a5e50d844838c1"                            
                        },
                        "valid": true
                    },
                    {
                        "service": {
                            "_id": "5a609f7ac9a5e50d844838c1"
                        }
                        "valid": false
                    }
                ],
                "valid": true
           },
           {
             "service_category": {
                    "_id": "5a609b4ac9a5e50d844838c0"
             },
             "sub_services": [
                    {
                        "service": {
                            "_id": "5a609f84c9a5e50d844838c2"
                        }
                        "valid": true
                    }
             ],
             "valid": false
           }
       ]
    }
}

预期结果

{
    "_id": "5a62ea5d7515222464e20016",
    "services": {
        "categories": [
            {
                "service_category": {
                    "_id": "5a609b40c9a5e50d844838bf"
                },
                "sub_services": [
                    {
                        "service": {
                            "_id": "5a609f7ac9a5e50d844838c1"                            
                        },
                        "valid": true
                    }
                ],
                "valid": true
             }
           ]
    }
}

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

使用$filter表达式过滤类别作为$map的输入,以使用$filter ed sub_services映射输出值。

 Vendor.aggregate([
  {"$match":{"_id":mongoose.Types.ObjectId(req.user._id), "services.categories.valid": true, "services.categories.sub_services.valid": true}},
  {
    "$addFields": {
      "services.categories": {
        "$map": {
          "input": {
            "$filter": {
              "input": "$services.categories",
              "as": "categoryf",
              "cond": "$$categoryf.valid"
            }
          },
          "as": "categorym",
          "in": {
            "service_category": "$$categorym.service_category",
            "valid": "$$categorym.valid",
            "sub_services":{
                 "$filter": {
                 "input": "$$categorym.sub_services",
                 "as": "sub_services",
                "cond": "$$sub_services.valid"
               }
             }
          }
        }
      }
    }
  }
])