Search code examples
mongodbgomongodb-querymongo-go

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

I need to remove empty sub arrays. I need to code this query in 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. So I'm guessing there is some problem with the nupdate. What am I doing wrong ?

The UpdateOne function is part of the mongo-driver for go lang


Solution

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