【问题标题】:MongoDB Aggregation - Change a value of field based on other field value in deeply nested array of objectsMongoDB Aggregation - 根据深度嵌套的对象数组中的其他字段值更改字段值
【发布时间】:2022-11-07 08:57:22
【问题描述】:

所以我的收藏中有很多文件。每个对象都是一个用户对象,其中包含想法和想法有回复。我想要的是当回复匿名为真时,它的用户名值应该说匿名而不是用户名值。

文档

[
  {
    "_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"
          }
        ]
      }
    ]
  }
]

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

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    有点复杂的查询。

    1. $set - 更新 thoughts 字段。

      1.1。 $map - 迭代每个 thought 文档并从中返回新文档1.1.1.

      1.1.1。 $mergeObjects - 合并 thoughts 文档和 replies 数组的对象1.1.1.1.

      1.1.1.1。 $map - 迭代reply 文档,通过合并reply 文档和username 字段返回新文档,并根据anonymous 字段通过$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

    【讨论】:

      猜你喜欢
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 2023-03-30
      • 2023-03-18
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多