【问题标题】:Filter nested of nested in Mongoose Populate过滤嵌套的嵌套在猫鼬填充
【发布时间】:2021-11-07 03:38:09
【问题描述】:

我有一个如下所示的对象:(即 Mongoose 查询的输出)

let systems = [
    {
        "maxUserLevel": 1,
        "subsystems": [
            {
                "sections": [],
                "name": "apple"
            },
            {
                "sections": [
                    {
                        "name": "banana"
                    }
                ],
                "name": "sun",
            },
            {
                "sections": [],
                "name": "orange"
            }
        ],
        "systemID": "12345"
    },
    {
        "maxUserLevel": 3,
        "subsystems": [
            {
                "sections": [],
                "name": "blue"
            },
            {
                "sections": [
                    {
                        "name": "pink"
                    }
                ],
                "name": "red",
            },
        ],
        "systemID": "15654"
    }];

Mongoose 查询:

this.model.System.find({username: user.username}, {
    _id: 0,
    allowedOrganizations: 0,
    name: 0,
    updatedAt: 0,
    createdAt: 0,
    versionKey: 0
})
    .populate(
        {
            path: "subsystems",
            populate: {
                path: "sections",
                select: "name -_id",
                match: {
                    allowedUsers: user.id
                }
            },
            select: "name metadata -_id",
        }
    )
    .exec((error, systems) => {
        return res.status(200).json({
            data: systems,
            success: true
        });
    });

我正在寻找一种方法来删除没有sectionssubsystems。 经过数小时的搜索,我认为无法根据嵌套的populate 过滤populate,所以我尝试了以下一些方法:

if (systems.subsystems.length > 0) {
    let test = [];
    systems.subsystems.forEach((value, index) => {
        if (value.sections.length !== 0) {
            test[index] = value;
        }
        if (systems.subsystems.length === index + 1) {
            return test;
        }
    })
}

但我不确定这是否正确。

【问题讨论】:

    标签: javascript node.js mongoose mongoose-populate


    【解决方案1】:

    我不确定这是不是最好的方法,但它解决了我的问题:

    const _ = require('lodash');
    
    systems.forEach((value, index) => {
        systems[index].subsystems = _.filter(value.subsystems,
            item => !item.sections.length == 0
        );
    
        if (systems.length === index + 1) {
            return systems;
        }
    });
    

    它会删除所有没有sectionssubsystems

    【讨论】:

      【解决方案2】:

      您可以像这样使用$filter 的聚合查询:

      db.collection.aggregate([
        {
          "$project": {
            "_id": 1,
            "maxUserLevel": 1,
            "subsystems": {
              "$filter": {
                "input": "$subsystems",
                "as": "s",
                "cond": {
                  "$ne": [
                    "$$s.sections",
                    []
                  ]
                }
              }
            }
          }
        }
      ])
      

      例如here

      您的查询还应包含$match 阶段(如您的find 阶段)和$lookup

      【讨论】:

        猜你喜欢
        • 2015-02-09
        • 2014-07-28
        • 2021-12-25
        • 2015-11-24
        • 1970-01-01
        • 2014-09-16
        • 1970-01-01
        • 2019-01-16
        相关资源
        最近更新 更多