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

100
Vistas
Adding an element to a sub array of an array in MongoDB

I want to create a mongodb document structure like below.

data:[[{a:b},{b:c}],[{e:f}],[{f:g},{j:h},{i:l}]]

This structure will allow me to add new sub array element, 4th one to the existing data array which contains three sub arrays in the above example, if required.

I am able to add new sub array element using below command from mongodb shell.

db.xyz.update({'id':'A01'},{$push:{data:[]}},{})

However, I am unable to findout solution for pushing data in to respective arrays. The following command is not working.

db.xyz.update({'id':'A01'},{$push:{data[0]:{$push:{a:b}}}},{})
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

A single $push should be enough. Try this instead:

db.xyz.update({'id':'A01'}, {$push: {"data.0": {a: "b"}}})

result:

{
   "_id":ObjectId("586b907a4979a26deaacf4ad"),
   "data":[
      [
         {
            "a":"b"
         },
         {
            "b":"c"
         },
         {
            "a":"b"
         }
      ],
      [
         {
            "e":"f"
         }
      ],
      [
         {
            "f":"g"
         },
         {
            "j":"h"
         },
         {
            "i":"l"
         }
      ]
   ]
}
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