【问题标题】:Nodejs mongoose select objects with in nested arrays based on object propertiesNodejs mongoose根据对象属性选择嵌套数组中的对象
【发布时间】:2018-07-02 09:47:18
【问题描述】:

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

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

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

架构

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
             }
           ]
    }
}

谁能帮帮我。

【问题讨论】:

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


    【解决方案1】:

    使用$filterexpression 过滤类别作为$map 的输入,以将输出值映射到$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"
                   }
                 }
              }
            }
          }
        }
      }
    ])
    

    【讨论】:

    • 嗨,我如何填充 services.categories.service_category 和 services.categories.service_category.sub_services.service @Veeram
    • 喜欢从不同的集合中填充?如果是,那么您有两个选项 mongoose populate 或 mongodb server lookup
    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 2018-08-21
    • 2022-09-23
    相关资源
    最近更新 更多