使用聚合框架根据 mongodb 中 objects 的深层嵌套数组中的其他字段值更改字段值

Change a value of field based on other field value in deeply nested array of objects in mongodb using aggregation framework

所以我的 collection 中有很多文档。每个 object 是一个用户 object,其中包含想法和想法有回复。我想要的是当回复为 anonymous true 时,它​​的用户名值应该是 anonymous 而不是用户名值。

文档

[
  {
    "_id": {
      "$oid": "6276eb2195b181d38eee0b43"
    },
    "username": "abvd",
    "password": "efgh",
    "thoughts": [
      {
        "_id": {
          "$oid": "62778ff975e2c8725b9276f5"
        },
        "text": "last  thought",
        "anonymous": true,
        "replies": [
          {
            "_id": {
              "$oid": "62778fff75e2c8725b9276f5"
            },
            "text": "new reply",
            "anonymous": true,
            "username": "cdf"
          },
          {
            "_id": {
              "$oid": "62778fff75e2c8725b9276f5"
            },
            "text": "new reply",
            "anonymous": false,
            "username": "cdf"
          }
        ]
      }
    ]
  }
]

需要输出。如果您看到用户名中的值显示为匿名,即使现有文档的值是 cdf

[
  {
    "_id": {
      "$oid": "6276eb2195b181d38eee0b43"
    },
    "username": "abvd",
    "password": "efgh",
    "thoughts": [
      {
        "_id": {
          "$oid": "62778ff975e2c8725b9276f5"
        },
        "text": "last  thought",
        "anonymous": true,
        "replies": [
          {
            "_id": {
              "$oid": "62778fff75e2c8725b9276f5"
            },
            "text": "new reply",
            "anonymous": true,
            "username": "anonymous"
          },
          {
            "_id": {
              "$oid": "62778fff75e2c8725b9276f5"
            },
            "text": "new reply",
            "anonymous": false,
            "username": "cdf"
          }
        ]
      }
    ]
  }
]

如果您知道如何提供帮助,请告诉我 这是包含现有文档的 mongo db playground url https://mongoplayground.net/p/WoP-3z-DMuf

有点复杂的查询。

  1. $set - 更新 thoughts 字段。

    1.1。 $map - 从 1.1.1.

    迭代每个 thought 文档和 return 新文档

    1.1.1。 $mergeObjects - 合并来自 1.1.1.1.

    thoughts 文档和 replies 数组的对象

    1.1.1.1。 $map - 通过合并 reply 文档和 username 字段并根据 anonymous 更新其值来迭代 reply 文档,return 新文档通过 $cond.

    字段
db.collection.aggregate([
  {
    $set: {
      thoughts: {
        $map: {
          input: "$thoughts",
          as: "thought",
          in: {
            $mergeObjects: [
              "$$thought",
              {
                replies: {
                  $map: {
                    input: "$$thought.replies",
                    as: "reply",
                    in: {
                      $mergeObjects: [
                        "$$reply",
                        {
                          username: {
                            "$cond": {
                              "if": "$$reply.anonymous",
                              "then": "anonymous",
                              "else": "$$reply.username"
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
])

Sample Mongo Playground