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

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

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

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:

确定