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

398
Vistas
Update element in array if exists else insert new element in that array in MongoDb

I'm having group of array of element in MongoDB as given below :

{ 
    "_id" : 5, 
    "quizzes" : [
        {
            "wk" : 1, 
            "score" : 10
        }, 
        {
            "wk" : 2, 
            "score" : 8
        }, 
        {
            "wk" : 3, 
            "score" : 5
        }
      ], 
    "play" : [
        {
            "wk" : 2, 
            "score" : 8
        }, 
        {
            "wk" : 3, 
            "score" : 5
        }
    ]
}

I am trying insert new record in array if not present and if record present in that array then update that array record. Below is my MongoDB query.

db.push.update(
    { _id: 5 },
    { $push: { "quizzes": {"wk" : 6.0,"score" : 8.0},"play": {"wk" : 6.0,"score" : 8.0}  } }
)

Every time when i execute this query it inserts new record in array but i want if record present then update that array.

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

0

Use $addToSet instead of $push.

db.push.update(
    { _id: 5 },
    { $addToSet: { "quizzes": {"wk": 6.0, "score": 8.0}, "play": {"wk": 6.0, "score": 8.0} } }
)

EDIT:

There is no simple built-in approach for conditional sub-document update in an array field, by specific property. However, a small trick can do the job by executing two commands in sequence.

For example: If we want to update the quizzes field with the object { "wk": 7.0, "score": 8.0 }, we can do it in two steps:

Step-1: $pull out sub-documents from the quizzes array where "wk": 7.0. (Nothing happens if matching sub-document not found).

db.push.update(
    { _id: 5 },
    { $pull: { "quizzes": { "wk": 7.0 } } }
)

Step-2: $addToSet the sub-document.

db.push.update(
    { _id: 5 },
    { $addToSet: { "quizzes": {"wk": 7.0, "score": 8.0} } }
)

You can combine the above two update commands using the bulk.find().update()

over 4 years ago · Santiago Trujillo Denunciar

0

I assume what you want to do is, add a property to an element of the array based on the same element's different property's value.

You can use the updateOne() function with the $(update) operator, please refer the document mongodb documentation for $(update).

Let's say you want to add a property description to the array element where the value of wk is 1 (assuming its some kind of key based on what you have provided)

Input: Your data collection given in the question.

Mongo Command:

db.<your_collection_name>.updateOne(
{
 "_id":5,
   "quizzes.wk":1
  },
  {
   $set:{
    "quizzes.$.description":"I am a new property"
  }
});

Output:

{
        "_id" : 5,
        "quizzes" : [
                {
                        "wk" : 1,
                        "score" : 10,
                        "description" : "I am a new property"
                },
                {
                        "wk" : 2,
                        "score" : 8
                },
                {
                        "wk" : 3,
                        "score" : 5
                }
        ],
        "play" : [
                {
                        "wk" : 2,
                        "score" : 8
                },
                {
                        "wk" : 3,
                        "score" : 5
                }
        ]
}

Note: It will only change one entry of an array, the one which is matched first.

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