Issue
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 ?
Solution
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.
Answered By – rrrrr
Answer Checked By – Pedro (GoLangFix Volunteer)