Mongo-转到如何使用 arrayFilter 在 "array of objects inside array of objects" 中查找元素
Mongo-go how to use arrayFilter to find elem in "array of objects inside array of objects"
让我们想象一个 JSON 像:
"user": {
"id": "1234",
...some fields,
"achievements": [
{
"scope": "life achievements",
"list": [
{"_id": 1, "title": "some text", "gotAt": "some date"},
{"_id": 2, "title": "some other text", "gotAt": "some date"}
]
},
{
"scope": "sport achievements",
"list": [
{"_id": 1, "title": "sport", "gotAt": "some date"},
{"_id": 2, "title": "some other sport", "gotAt": "some date"}
]
},
{
"scope": "academic achievements",
"list": [
{"_id": 1, "title": "some text", "gotAt": "some date"},
{"_id": 2, "title": "some other text", "gotAt": "some date"}
]
},
]
}
我需要使用 mongoDb 和 Golang 驱动程序 Mongo 更新第一个人生成就(id 为 1)- 去做吧
问题出在嵌套级别。我可以通过 $ mongoDb 运算符获得一些数组元素,但在这里我需要通过 activity 范围(生活、运动、学术等)和成就的 _id 字段来识别每个用户成就。所以我需要在其他对象数组中的对象数组中进行搜索。
我就是这么干的(反正不管用)
doc := coll.FindOneAndUpdate(
context.Background(),
bson.D{
{"_id", "1234"},
{"achievements.scope", "life achievements"},
{"achievements.list._id", 1},
},
bson.D{
{"$set", bson.D{
{"achievements.$.list.$[elem].title", "some new test"},
}},
},
options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
Filters: append(make([]interface{}, 0), bson.D{
{"elem", bson.D{
{"achievements.list._id", 1},
}},
}),
}).SetReturnDocument(1),
)
我希望此查询仅更新 "id" 和 "scope" 字段指定的一个元素。可能是我不正确地使用了 arrayFilters,也可能是我错误地设置了 FindOneAndUpdate 函数的参数。现在它不会抛出任何错误,也不会更新文档。请帮助我
使用包 go.mongodb.org/mongo-driver/mongo
你可以这样做:
coll.FindOneAndUpdate(
context.Background(),
bson.D{
{"id", "1234"},
{"achievements.scope", "life achievements"},
{"achievements.list._id", 1},
},
bson.M{"$set": bson.M{"achievements.$.list.$[elem].title": "some new test"}},
options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
Filters: []interface{}{bson.M{"elem._id": 1}},
}),
)
“翻译”为 shell:
db.user.findOneAndUpdate(
{"id": "1234","achievements.scope": "life achievements","achievements.list._id": 1},
{$set: {"achievements.$.list.$[elem].title": "some new test"}},
{arrayFilters: [{"elem._id": 1}]}
)
因此,在@owlwalks 的帮助下,我们找到了决定:
doc := coll.FindOneAndUpdate(
context.Background(),
bson.D{
{"_id", "1234"},
{"achievements.scope", "life achievements"},
},
bson.D{
{"$set", bson.D{
{"achievements.$.list.$[elem].title", "some new text"},
}},
},
options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
Filters: []interface{}{bson.D{
{"elem._id", 1},
}},
}).SetReturnDocument(1),
)
让我们想象一个 JSON 像:
"user": {
"id": "1234",
...some fields,
"achievements": [
{
"scope": "life achievements",
"list": [
{"_id": 1, "title": "some text", "gotAt": "some date"},
{"_id": 2, "title": "some other text", "gotAt": "some date"}
]
},
{
"scope": "sport achievements",
"list": [
{"_id": 1, "title": "sport", "gotAt": "some date"},
{"_id": 2, "title": "some other sport", "gotAt": "some date"}
]
},
{
"scope": "academic achievements",
"list": [
{"_id": 1, "title": "some text", "gotAt": "some date"},
{"_id": 2, "title": "some other text", "gotAt": "some date"}
]
},
]
}
我需要使用 mongoDb 和 Golang 驱动程序 Mongo 更新第一个人生成就(id 为 1)- 去做吧
问题出在嵌套级别。我可以通过 $ mongoDb 运算符获得一些数组元素,但在这里我需要通过 activity 范围(生活、运动、学术等)和成就的 _id 字段来识别每个用户成就。所以我需要在其他对象数组中的对象数组中进行搜索。
我就是这么干的(反正不管用)
doc := coll.FindOneAndUpdate(
context.Background(),
bson.D{
{"_id", "1234"},
{"achievements.scope", "life achievements"},
{"achievements.list._id", 1},
},
bson.D{
{"$set", bson.D{
{"achievements.$.list.$[elem].title", "some new test"},
}},
},
options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
Filters: append(make([]interface{}, 0), bson.D{
{"elem", bson.D{
{"achievements.list._id", 1},
}},
}),
}).SetReturnDocument(1),
)
我希望此查询仅更新 "id" 和 "scope" 字段指定的一个元素。可能是我不正确地使用了 arrayFilters,也可能是我错误地设置了 FindOneAndUpdate 函数的参数。现在它不会抛出任何错误,也不会更新文档。请帮助我
使用包 go.mongodb.org/mongo-driver/mongo
你可以这样做:
coll.FindOneAndUpdate(
context.Background(),
bson.D{
{"id", "1234"},
{"achievements.scope", "life achievements"},
{"achievements.list._id", 1},
},
bson.M{"$set": bson.M{"achievements.$.list.$[elem].title": "some new test"}},
options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
Filters: []interface{}{bson.M{"elem._id": 1}},
}),
)
“翻译”为 shell:
db.user.findOneAndUpdate(
{"id": "1234","achievements.scope": "life achievements","achievements.list._id": 1},
{$set: {"achievements.$.list.$[elem].title": "some new test"}},
{arrayFilters: [{"elem._id": 1}]}
)
因此,在@owlwalks 的帮助下,我们找到了决定:
doc := coll.FindOneAndUpdate(
context.Background(),
bson.D{
{"_id", "1234"},
{"achievements.scope", "life achievements"},
},
bson.D{
{"$set", bson.D{
{"achievements.$.list.$[elem].title", "some new text"},
}},
},
options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
Filters: []interface{}{bson.D{
{"elem._id", 1},
}},
}).SetReturnDocument(1),
)