Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Remove array element in MongoDB in Go

I have data in MongoDB like below:

{"_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"}

And I have Mongo shell to remove DHBK1_FUNC_1 element from array.

Here is my Mongo shell:

db.company_role_function.update(
  { },
  { $pull: { functioncodelist: { $in: ['DHBK1_FUNC_1'] }}},
  { multi: true }
)

Then I write Go code to implement my Mongo shell.

Here is my code:

    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)
    }
}

When I run my code, it showed this error:

# 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 {})

When I remove bson.M{"multi": true} in line err = c.Update(bson.M{}, bson.M{"$pull": bson.M{"functioncodelist": bson.M{"$in": functionCode}}}, bson.M{"multi": true}), it worked but doesn’t remove any element DHBK1_FUNC_1.

Thank you

Hi @Napoleon_Ponaparte,

Based on your code snippet, looks like you’re using mgo/v2 instead of the MongoDB official mongo-go-driver. The problem is caused by the option to specify multiple updates i.e. {multi: true} option.

Using mongo-go-driver you should be able to utilise Collection.UpdateMany(). For example:

collection := client.Database("users").Collection("company_role_function")
filter := bson.M{}
statement := bson.M{"$pull": bson.M{"functioncodelist": bson.M{"$in": bson.A{"DHBK1_FUNC_1"}}}}
result, err := collection.UpdateMany(ctx, filter, statement)

If you’re using mgo please see Collection.UpdateAll() instead. Please note that mgo only has full support up to MongoDB server v3.6. If you’re starting a new project, I’d recommend to use mongo-go-driver.

Regards,
Wan.

1 Like

thank you for your help

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.