在 Go 中删除 MongoDB 中的数组元素
Remove array element in MongoDB in Go
我在 MongoDB 中有如下数据:
{"_id":{"$oid":"5ed0cb4cb8d5570916d1ee7e"},"rolecode":"DHBK1_ROLE_05","productid":"XYZ_Platform","functioncodelist":["DHBK1_FUNC_1","DHBK1_FUNC_2","DHBK1_FUNC_3","DHBK1_FUNC_4"],"comid":"DHBK1"}
{"_id":{"$oid":"5ed0cc67b8d5570916d1ef86"},"rolecode":"DHBK1_ROLE_06","productid":"LAM_Platform","functioncodelist":["DHBK1_FUNC_1","DHBK1_FUNC_2","DHBK1_FUNC_3"],"comid":"DHBK1"}
{"_id":{"$oid":"5ed0d23cb8d5570916d1f4c8"},"rolecode":"DHBK1_ROLE_09","productid":"LAM_Platform","functioncodelist":["DHBK1_FUNC_1"],"comid":"DHBK1"}
我有 Mongo shell 从数组中删除 DHBK1_FUNC_1
元素。
这是我的 Mongo shell:
db.company_role_function.update(
{ },
{ $pull: { functioncodelist: { $in: ['DHBK1_FUNC_1'] }}},
{ multi: true }
)
然后我写Go代码来实现我的Mongo shell.
这是我的代码:
package main
import (
"context"
"fmt"
"strings"
"time"
"gopkg.in/mgo.v2"
)
func main() {
var functionCode []string
functionCode = append(functionCode, "DHBK1_FUNC_1")
fmt.Println(functionCode)
deleteArray(functionCode)
}
func deleteArray(functionCode []string) {
session, err := mgo.Dial("mongo_uri_connect")
if err != nil {
panic(err)
}
c := session.DB("users").C("company_role_function")
err = c.Update(bson.M{}, bson.M{"$pull": bson.M{"functioncodelist": bson.M{"$in": functionCode}}}, bson.M{"multi": true})
if err != nil {
fmt.Println(err)
}
}
当我 运行 我的代码时,它显示了这个错误:
# command-line-arguments
.\main.go:86:16: too many arguments in call to c.Update
have (primitive.M, primitive.M, primitive.M)
want (interface {}, interface {})
当我删除行 err = c.Update(bson.M{}, bson.M{"$pull": bson.M{"functioncodelist": bson.M{"$in": functionCode}}}, bson.M{"multi": true})
中的 bson.M{"multi": true}
时,它起作用了,但没有删除任何元素 DHBK1_FUNC_1
。
谢谢
我尝试使用 Mongo-go-driver 并且成功了。
但是Mgo还是不行
这是我的代码:
func deleteArrayDriver(functionCode []string) {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("link"))
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
database := client.Database("users")
users := database.Collection("company_role_function")
_, err = users.UpdateMany(ctx, bson.D{{}}, bson.M{"$pull": bson.M{"functioncodelist": bson.M{"$in": functionCode}}})
if err != nil {
panic(err)
}
}
我在 MongoDB 中有如下数据:
{"_id":{"$oid":"5ed0cb4cb8d5570916d1ee7e"},"rolecode":"DHBK1_ROLE_05","productid":"XYZ_Platform","functioncodelist":["DHBK1_FUNC_1","DHBK1_FUNC_2","DHBK1_FUNC_3","DHBK1_FUNC_4"],"comid":"DHBK1"}
{"_id":{"$oid":"5ed0cc67b8d5570916d1ef86"},"rolecode":"DHBK1_ROLE_06","productid":"LAM_Platform","functioncodelist":["DHBK1_FUNC_1","DHBK1_FUNC_2","DHBK1_FUNC_3"],"comid":"DHBK1"}
{"_id":{"$oid":"5ed0d23cb8d5570916d1f4c8"},"rolecode":"DHBK1_ROLE_09","productid":"LAM_Platform","functioncodelist":["DHBK1_FUNC_1"],"comid":"DHBK1"}
我有 Mongo shell 从数组中删除 DHBK1_FUNC_1
元素。
这是我的 Mongo shell:
db.company_role_function.update(
{ },
{ $pull: { functioncodelist: { $in: ['DHBK1_FUNC_1'] }}},
{ multi: true }
)
然后我写Go代码来实现我的Mongo shell.
这是我的代码:
package main
import (
"context"
"fmt"
"strings"
"time"
"gopkg.in/mgo.v2"
)
func main() {
var functionCode []string
functionCode = append(functionCode, "DHBK1_FUNC_1")
fmt.Println(functionCode)
deleteArray(functionCode)
}
func deleteArray(functionCode []string) {
session, err := mgo.Dial("mongo_uri_connect")
if err != nil {
panic(err)
}
c := session.DB("users").C("company_role_function")
err = c.Update(bson.M{}, bson.M{"$pull": bson.M{"functioncodelist": bson.M{"$in": functionCode}}}, bson.M{"multi": true})
if err != nil {
fmt.Println(err)
}
}
当我 运行 我的代码时,它显示了这个错误:
# command-line-arguments
.\main.go:86:16: too many arguments in call to c.Update
have (primitive.M, primitive.M, primitive.M)
want (interface {}, interface {})
当我删除行 err = c.Update(bson.M{}, bson.M{"$pull": bson.M{"functioncodelist": bson.M{"$in": functionCode}}}, bson.M{"multi": true})
中的 bson.M{"multi": true}
时,它起作用了,但没有删除任何元素 DHBK1_FUNC_1
。
谢谢
我尝试使用 Mongo-go-driver 并且成功了。
但是Mgo还是不行
这是我的代码:
func deleteArrayDriver(functionCode []string) {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("link"))
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
database := client.Database("users")
users := database.Collection("company_role_function")
_, err = users.UpdateMany(ctx, bson.D{{}}, bson.M{"$pull": bson.M{"functioncodelist": bson.M{"$in": functionCode}}})
if err != nil {
panic(err)
}
}