【问题标题】:How to use projection for nested value in mongod如何在mongodb中对嵌套值使用投影
【发布时间】:2021-12-16 19:51:45
【问题描述】:

如何使用投影从集合的所有文档中仅查看以下部分?

条件: 我只需要获取 "type": "DEBIT" 和以下 2 行,而不是同一类型的所有其他键。 我不想查看其他类型,例如帐户、存款。

{
     "key": "Call",
     "enabled": true,
 }

以下结构中的示例文档。

{
    "_id": "1",
    "menu": [
        {
            "type": "ACCOUNT",
            "scope": "ACCOUNT",
            "items": [
                {
                    "key": "Call",
                    "enabled": true,
                },
                {
                    "key": "Work",
                    "enabled": true,
                }
            ]
        },
        {
            "type": "DEPOSIT",
            "scope": "DEPOSIT",
            "items": [
               {
                    "key": "Call",
                    "enabled": true,
                },
                {
                    "key": "Work",
                    "enabled": true,
                }
            ]
        },
        {
            "type": "DEBIT",
            "scope": "DEBIT",
            "items": [
               {
                    "key": "Call",
                    "enabled": true,
                },
                {
                    "key": "Work",
                    "enabled": true,
                }
            ]
        },
    ]
}

【问题讨论】:

    标签: arrays mongodb mongoose mongodb-query pymongo


    【解决方案1】:

    使用$filter

    db.collection.aggregate([
      {
        "$match": {
          "menu.type": "DEBIT"
        }
      },
      {
        "$set": {
          "menu": {
            "$filter": {
              "input": "$menu",
              "as": "m",
              "cond": {
                $eq: [
                  "$$m.type",
                  "DEBIT"
                ]
              }
            }
          }
        }
      }
    ])
    

    mongoplayground

    【讨论】:

    • 谢谢@YuTing 它有效。我错过了更新的一半。非常感谢您的帮助
    【解决方案2】:

    首先,你需要$unwind菜单 然后 $match 类型借记 并过滤数组项,然后分组以创建最终结果

    db.collection.aggregate([
      {
        "$unwind": "$menu"
      },
      {
        $match: {
          "menu.type": "DEBIT"
        }
      },
      {
        "$project": {
          _id: 1,
          "menu.items": {
            "$filter": {
              "input": "$menu.items",
              "as": "s",
              "cond": {
                $and: [
                  {
                    $eq: [
                      "$$s.enabled",
                      true
                    ]
                  },
                  {
                    $eq: [
                      "$$s.key",
                      "Call"
                    ]
                  }
                ]
              }
            }
          }
        }
      },
      {
        "$group": {
          "_id": "$_id",
          "menu": {
            "$push": "$menu"
          }
        }
      }
    ])
    

    https://mongoplayground.net/p/kRChgF9rLsI

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      相关资源
      最近更新 更多