I want to insert to many values into array but i can't because the insertion agregate to []

have to document.

({
  "comercio": {
    "descripcion": "Realizar pruebas"
  },
  "contacto": {
    "correo": "alexis@com.mx"
  },
  "cuenta": {
    "nombreCompletoTitular": "Alexis Francisco ",
    "numeroCuenta": "0000000000"
  },
  "diasLaborales": [
    {
      "dia": "Lunes",
      "horarios": {
        "horaApertura": "07:00:00",
        "horaCierre": "23:00:00"
      }
    },
    {
      "dia": "Martes",
      "horarios": {
        "horaApertura": "07:00:00",
        "horaCierre": "23:00:00"
      }
    }
  ],
  "domicilio": {
    "calle": "combate de leon",
    "codigoPostal": "09200",
    "idColonia": NumberLong(1),
    
  }
})

Where i need to insert to many values in my array “diasLaborales”, but my querie dont allow.

db.TablaEjemploDoc.update(
{
    "_id": ObjectId("639a5cf73a78e35ff9787f32")
},
{
    "$push": {
        "diasLaborales":
        {
            "dia": "Miercoles",
            "horarios": {
                "horaApertura": "09:00:00",
                "horaCierre": "15:00:00"
            }
        },
        {
            "dia": "Jueves",
            "horarios": {
                "horaApertura": "09:00:00",
                "horaCierre": "15:00:00"
            }
        },
    }
});

this return a error.

I hope that this response that querie return must be :

({
  "comercio": {
    "descripcion": "Realizar pruebas"
  },
  "contacto": {
    "correo": "alexis@com.mx"
  },
  "cuenta": {
    "nombreCompletoTitular": "Alexis Francisco ",
    "numeroCuenta": "0000000000"
  },
  "diasLaborales": [
    {
      "dia": "Lunes",
      "horarios": {
        "horaApertura": "07:00:00",
        "horaCierre": "23:00:00"
      }
    },
    {
      "dia": "Martes",
      "horarios": {
        "horaApertura": "07:00:00",
        "horaCierre": "23:00:00"
      }
    },
   {
      "dia": "Miercoles",
      "horarios": {
        "horaApertura": "07:00:00",
        "horaCierre": "23:00:00"
      }
    },
    {
      "dia": "Jueves",
      "horarios": {
        "horaApertura": "07:00:00",
        "horaCierre": "23:00:00"
      }
    },
  ],
  "domicilio": {
    "calle": "combate de leon",
    "codigoPostal": "09200",
    "idColonia": NumberLong(1),
    
  }
})

Hi @Lourdes_Nataly_Rojas_Hernandez,

You update query is incorrect indeed.

You must use $push with $each.

db.TablaEjemploDoc.updateOne(
   { _id: ObjectId("639a5cf73a78e35ff9787f32") },
   { $push: { diasLaborales: { $each: [ {your}, {docs}, {here} ] } } }
)

Cheers,
Maxime.

3 Likes

Thanks so much!.
Your information is grath and of much help!!

1 Like