英文:
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
我会这样做,使用objectToArray
和unwind
来逐层查找,直到最终使用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
.
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"
}
}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论