从多个数组中使用$in过滤器提取对象。

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

$pull objects from multiple array with $in filter

问题

我有以下的user文档:

  1. {
  2. "_id":{
  3. "$oid":"627cc1add375d47675b8104a"
  4. },
  5. "email":"camille.balzac@outlook.fr",
  6. "username":"camille.balzac",
  7. "password":"9394516cc9354d3ef1a37a88dfefc364182a80c59a147ba77b240ac78ca5e788",
  8. "salt":"xooRCeSUh9KVmGH",
  9. "isteacher":false,
  10. "instances":[
  11. {
  12. "instanceid":"i-0c7a14f4bebaxxxxx",
  13. "identifier":"t1u4sm2abyc3rdkhevrgs0tg5592idjp",
  14. "teacherid":{
  15. "$oid":"627cc1a9d375d47675b81049"
  16. }
  17. },
  18. {
  19. "instanceid":"i-0e4c61434a85xxxxx",
  20. "identifier":"pqqloehk4oeiy2ofh4d7tsn6j281wnbe",
  21. "teacherid":{
  22. "$oid":"627cc1a9d375d47675b81049"
  23. }
  24. }
  25. ]
  26. }

我正在尝试构建一个Go函数,该函数将删除所有instances实体,其id在作为参数(instancesIds)传递的切片中。
目前它看起来是这样的:

  1. func DeleteInstancesById(instancesIds []string) error {
  2. res, err := usersCollection.UpdateOne(
  3. context.TODO(),
  4. bson.M{},
  5. bson.M{
  6. "$pull": bson.M{
  7. "instances": bson.M{
  8. "instanceid": bson.M{
  9. "$in": instancesIds,
  10. },
  11. },
  12. },
  13. },
  14. )
  15. }

例如,这意味着如果我像这样调用函数:

  1. DeleteInstancesById([]string{"i-0c7a14f4bebaxxxxx"})

它应该删除所有id为"i-0c7a14f4bebaxxxxx"的实例。

问题是尽管我尝试了很多次,但没有进行任何更新。有什么想法吗?

英文:

I have the following user document :

  1. {
  2. "_id":{
  3. "$oid":"627cc1add375d47675b8104a"
  4. },
  5. "email":"camille.balzac@outlook.fr",
  6. "username":"camille.balzac",
  7. "password":"9394516cc9354d3ef1a37a88dfefc364182a80c59a147ba77b240ac78ca5e788",
  8. "salt":"xooRCeSUh9KVmGH",
  9. "isteacher":false,
  10. "instances":[
  11. {
  12. "instanceid":"i-0c7a14f4bebaxxxxx",
  13. "identifier":"t1u4sm2abyc3rdkhevrgs0tg5592idjp",
  14. "teacherid":{
  15. "$oid":"627cc1a9d375d47675b81049"
  16. }
  17. },
  18. {
  19. "instanceid":"i-0e4c61434a85xxxxx",
  20. "identifier":"pqqloehk4oeiy2ofh4d7tsn6j281wnbe",
  21. "teacherid":{
  22. "$oid":"627cc1a9d375d47675b81049"
  23. }
  24. }
  25. ]
  26. }

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 :

  1. func DeleteInstancesById(instancesIds []string) error {
  2. res, err := usersCollection.UpdateOne(
  3. context.TODO(),
  4. bson.M{},
  5. bson.M{
  6. "$pull": bson.M{
  7. "instances": bson.M{
  8. "instanceid": bson.M{
  9. "$in": instancesIds,
  10. },
  11. },
  12. },
  13. },
  14. )
  15. }

For example, that means if I call the function like this :

  1. 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.

huangapple
  • 本文由 发表于 2022年5月12日 19:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/72214830.html
匿名

发表评论

匿名网友

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

确定