英文:
$pull objects from multiple array with $in filter
问题
我有以下的user
文档:
{
"_id":{
"$oid":"627cc1add375d47675b8104a"
},
"email":"camille.balzac@outlook.fr",
"username":"camille.balzac",
"password":"9394516cc9354d3ef1a37a88dfefc364182a80c59a147ba77b240ac78ca5e788",
"salt":"xooRCeSUh9KVmGH",
"isteacher":false,
"instances":[
{
"instanceid":"i-0c7a14f4bebaxxxxx",
"identifier":"t1u4sm2abyc3rdkhevrgs0tg5592idjp",
"teacherid":{
"$oid":"627cc1a9d375d47675b81049"
}
},
{
"instanceid":"i-0e4c61434a85xxxxx",
"identifier":"pqqloehk4oeiy2ofh4d7tsn6j281wnbe",
"teacherid":{
"$oid":"627cc1a9d375d47675b81049"
}
}
]
}
我正在尝试构建一个Go函数,该函数将删除所有instances
实体,其id在作为参数(instancesIds
)传递的切片中。
目前它看起来是这样的:
func DeleteInstancesById(instancesIds []string) error {
res, err := usersCollection.UpdateOne(
context.TODO(),
bson.M{},
bson.M{
"$pull": bson.M{
"instances": bson.M{
"instanceid": bson.M{
"$in": instancesIds,
},
},
},
},
)
}
例如,这意味着如果我像这样调用函数:
DeleteInstancesById([]string{"i-0c7a14f4bebaxxxxx"})
它应该删除所有id为"i-0c7a14f4bebaxxxxx"的实例。
问题是尽管我尝试了很多次,但没有进行任何更新。有什么想法吗?
英文:
I have the following user
document :
{
"_id":{
"$oid":"627cc1add375d47675b8104a"
},
"email":"camille.balzac@outlook.fr",
"username":"camille.balzac",
"password":"9394516cc9354d3ef1a37a88dfefc364182a80c59a147ba77b240ac78ca5e788",
"salt":"xooRCeSUh9KVmGH",
"isteacher":false,
"instances":[
{
"instanceid":"i-0c7a14f4bebaxxxxx",
"identifier":"t1u4sm2abyc3rdkhevrgs0tg5592idjp",
"teacherid":{
"$oid":"627cc1a9d375d47675b81049"
}
},
{
"instanceid":"i-0e4c61434a85xxxxx",
"identifier":"pqqloehk4oeiy2ofh4d7tsn6j281wnbe",
"teacherid":{
"$oid":"627cc1a9d375d47675b81049"
}
}
]
}
I'm trying to build a go function that would delete all instances
entities whose id is in the slice that's passed as an argument (instancesIds
).
For now it looks like this :
func DeleteInstancesById(instancesIds []string) error {
res, err := usersCollection.UpdateOne(
context.TODO(),
bson.M{},
bson.M{
"$pull": bson.M{
"instances": bson.M{
"instanceid": bson.M{
"$in": instancesIds,
},
},
},
},
)
}
For example, that means if I call the function like this :
DeleteInstancesById([]string{"i-0c7a14f4bebaxxxxx"})
It shoud delete all the instances whose id is "i-0c7a14f4bebaxxxxx".
The problem is that no update are made despite my numerous tries. Any idea ?
答案1
得分: 0
好的,以下是翻译好的内容:
好的,我真的不明白为什么,但我成功地通过将UpdateOne
函数替换为UpdateMany
函数使该函数工作。
这纯粹是运气,如果能给出任何解释,将不胜感激。
英文:
Ok I really don't understand why but I managed to make the function work by replacing the UpdateOne
function by the UpdateMany
function.
This was pure luck and any explaination would be greatly appreciated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论