在MongoDB中仅针对动态字段名嵌套对象的项目 – 原问题的扩展。

huangapple go评论57阅读模式
英文:

Project only nested objects in mongodb for dynamic field names - extension of original question

问题

我正在查询一个具有几乎与[此问题](https://stackoverflow.com/questions/61952309/project-only-nested-objects-in-mongodb-for-dynamic-field-names/61958290#61958290)中描述的数据结构相同的数据库,具有动态字段名称。

[此代码](https://mongoplayground.net/p/FxjR150tZhx) 在一个答案中提供了几乎完全符合我需求的功能,只是我想要投影task7中的所有字段(即count和time),而不仅仅是count。以下是代码的副本:

db.collection.aggregate([
{
$project: {
_id: 0,
tasks: {
$objectToArray: "$tasks"
}
}
},
{
$match: {
"tasks.v.task7": {
$exists: true
}
}
},
{
$project: {
tasks: {
$arrayToObject: {
$filter: {
input: {
$map: {
input: "$tasks",
in: {
"k": "$$this.k",
"v": "$$this.v.task7.count"
}
}
},
cond: "$$this.v"
}
}
}
}
}
])

有人能帮我调整代码,以便我可以检索多个字段吗?我一直在尝试,但我是一个新手,到目前为止没有成功。

更新:如果我可以获得输出,使文档嵌套在每个id下,并且看起来像下面这样,那将会很好:

{
"id": 1,
"name": "N1",
"tasks_project1": {
"k": "task7",
"v": {
"count": 10,
"time": 1.2
}
},
"tasks_project25": {
"k": "task7",
"v": {
"count": 25,
"time": 10
}
}
}

更新#2:Ruben在下面的评论中提供了提供所需输出的答案。
英文:

I am querying a database with a structure almost exactly like the data described in this question, with dynamic field names.

{
  id: 1,
  name: "N1",
  tasks: {
    project1: {
      task7: {
        count: 10
        time: 1.2
      },
      task3: {
        count: 56
        time: 1050
      }
    },
    project25: {
      task7: {
        count: 25
        time: 10
      }
    }
  }
}

This code provided in an answer does almost exactly what I need to do, except that I would like to project all fields within task7 (i.e., count and time), not just count. Code copy-pasted here:

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      tasks: {
        $objectToArray: "$tasks"
      }
    }
  },
  {
    $match: {
      "tasks.v.task7": {
        $exists: true
      }
    }
  },
  {
    $project: {
      tasks: {
        $arrayToObject: {
          $filter: {
            input: {
              $map: {
                input: "$tasks",
                in: {
                  "k": "$$this.k",
                  "v": "$$this.v.task7.count"
                }
              }
            },
            cond: "$$this.v"
          }
        }
      }
    }
  }
])

Could anyone help me tweak the code so that I can retrieve more than one field? I have been trying to, but I am a novice, and so far no luck.

UPDATE: It would be great if I could get the output so that documents are nested under each id, and look something like below:

  {
    "id": 1,
    "name": "N1",
    "tasks_project1": {
        "k": "task7",
        "v": {
          "count": 10,
          "time": 1.2
}
},
    "tasks_project25": {
        "k": "task7",
        "v": {
          "count": 25,
          "time": 10
        }
      }
  }

UPDATE #2: Ruben provided the answer in a comment below that provides the desired output.

答案1

得分: 1

我会这样做,使用objectToArrayunwind来逐层查找,直到最终使用match找到我想要的信息。

然后,您可以使用project按您的需求格式化输出。

示例

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      "id": 1,
      "name": 1,
      tasks: {
        $objectToArray: "$tasks"
      }
    }
  },
  {
    "$unwind": "$tasks"
  },
  {
    $project: {
      _id: 0,
      "id": 1,
      "name": 1,
      "tasks.k": 1,
      "tasks.v": {
        $objectToArray: "$tasks.v"
      }
    }
  },
  {
    "$unwind": "$tasks.v"
  },
  {
    "$match": {
      "tasks.v.k": "task7"
    }
  }
])
英文:

I'd do it like this, using objectToArray and unwind to go through each level until I can finally find the information I want with a match.

Then you could format your output as you want with a project.

Example

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      "id": 1,
      "name": 1,
      tasks: {
        $objectToArray: "$tasks"
      }
    }
  },
  {
    "$unwind": "$tasks"
  },
  {
    $project: {
      _id: 0,
      "id": 1,
      "name": 1,
      "tasks.k": 1,
      "tasks.v": {
        $objectToArray: "$tasks.v"
      }
    }
  },
  {
    "$unwind": "$tasks.v"
  },
  {
    "$match": {
      "tasks.v.k": "task7"
    }
  }
])

huangapple
  • 本文由 发表于 2023年6月16日 05:18:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定