Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

281
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda