[英]MongoDB update operation not working using golang mongo-driver
This is with reference to这是参考
How to remove a document inside an array in mongodb using $pull 如何使用 $pull 删除 mongodb 中数组内的文档
I need to remove empty sub arrays.我需要删除空子 arrays。 I need to code this query in go lang
我需要在 go lang 中编码这个查询
db.getCollection('workflows').update({<find condition>}, {$pull: {"workflows":[] } } )
So I've written the below code所以我写了下面的代码
nquery := bson.D {
{"level", "application"},
{"workflowName", workflowName},
{"applicationName", applicationName},
}
nupdate := bson.M{"$pull": bson.M{"workflows":"[]"}}
UpdateOne(getContext(), nquery, nupdate)
The result of UpdateOne shows my query has matched but hasn't modified anything. UpdateOne 的结果显示我的查询已匹配但未修改任何内容。 So I'm guessing there is some problem with the nupdate.
所以我猜nupdate有一些问题。 What am I doing wrong?
我究竟做错了什么?
The UpdateOne function is part of the mongo-driver for go lang UpdateOne function 是 go lang 的 mongo 驱动程序的一部分
"[]"
is not an empty array in the query, it's a string having an opening and closing square brackets. "[]"
在查询中不是一个空数组,它是一个带有左方括号和右方括号的字符串。
The MongoDB empty array can be "modeled" with an empty slice in Go, eg with a value of type []interface{}
, so a composite literal of this type is []interface{}{}
. MongoDB 空数组可以用 Go 中的空切片“建模”,例如使用
[]interface{}
类型的值,因此这种类型的复合文字是[]interface{}{}
。
So use this:所以使用这个:
nupdate := bson.M{"$pull": bson.M{"workflows": []interface{}{}}}
The square brackets "[]" shall not be in quotes since now they are interpreted as string... , they need to be added just as square brackets [] and require in golang: &[]int{} to be translated by the mongo goland driver to empty array...方括号 "[]" 不应放在引号中,因为现在它们被解释为字符串...,它们需要像方括号 [] 一样添加,并且在 golang 中要求:&[]int{} 由mongo goland驱动程序清空数组...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.