Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

"[]" 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, e.g. with a value of type []interface{}, so a composite literal of this type is []interface{}{}.

So use this:

nupdate := bson.M{"$pull": bson.M{"workflows": []interface{}{}}}
over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!