Node.js猫鼬根据对象属性选择嵌套数组中的对象 [英] Nodejs mongoose select objects with in nested arrays based on object properties

查看:111
本文介绍了Node.js猫鼬根据对象属性选择嵌套数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to nodejs and currently i'm trying to implement a web application using nodejs and mongodb (mongoose).

我想根据嵌套对象和父对象的属性选择对象.

I want to select objects within a nested array and parent array based on a property of those objects.

简而言之-我需要选择具有有效属性为true的类别和具有有效属性为true的sub_services. (在类别中,不能有具有有效属性为false的子服务,也不能有具有有效属性为false的类别对象.)

Simply - I need to select Categories which are having valid property as true and sub_services which are having valid property as true. (with in a category there cannot be sub_services which having valid property as false and there cannot be category objects which are having valid property as 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
             }
           ]
    }
}

有人可以帮我吗?

推荐答案

使用$filter表达式来过滤类别作为$map的输入,以映射具有$filter ed子服务的输出值.

Use $filterexpression to filter categories as input to $map to map the output values with $filtered 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"
               }
             }
          }
        }
      }
    }
  }
])

这篇关于Node.js猫鼬根据对象属性选择嵌套数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆