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

177
Vistas
Update last element of a nested array in mongodb

I have tried the below query of mongoose which does not seem to work:

Model.findOneAndUpdate(
    {
        name: origin
    },
    {
        $set: {
            'field1.$[id1].field2.-1': "value"
        }
    },
    {
        arrayFilters: [
            { 'id1.userId': "customerId" }
        ],
        new: true
    }
);

Note: field1 and field2 are arrays

The negative indexes are not accepted by MongoDB which is causing problems.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You may consider using the $set (aggregation) operator and use double $map operator:

db.collection.aggregate([
    { $match: { name: "myname" } }
    {
        $set: {
            field1: {
                $map: {
                    input: "$field1",
                    in: {
                        $cond: {
                            if: { $ne: [ "$$this.userId", "customerId" ] },
                            then: "$$this",
                            else: {
                                $mergeObjects: [
                                    "$$this",
                                    { 
                                        field2: { 
                                            $concatArrays: [
                                                { $slice: [ "$$this.field2", { $add: [ { $size: "$$this.field2" }, -1 ] } ] },
                                                ["value"]
                                            ]
                                        } 
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
])

Mongo Playground

over 4 years ago · Santiago Trujillo Denunciar

0

Apply the $set operator together with the $ positional operator in your update to change the name field.

The $ positional operator will identify the correct element in the array to update without explicitly specifying the position of the element in the array, thus your final update statement should look like:

db.collection.update(
    { "friends.u.username": "michael" }, 
    { "$set": { "friends.$.u.name": "hello" } }
)

Answer taken from - https://stackoverflow.com/a/34431571

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