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

117
Vistas
MongoDb push only if property not already in array

I'm trying to update a nested array in a document only if the array does not already include the item like so:

await userCollection.findOneAndUpdate(
    { _id: ObjectId('616acc597ebda90ca6ffee21') },
    {
      'devices': {
        $push: {
          $cond: {
            if: {
              $in: [req.body.serial, '$devices']
            },
            then: '$devices',
            else: { serial: req.body.serial, data: [] }
          }
        }
      }
    },
    { returnOriginal: false },
    (err, _) => {
      if (err) {
        return res.status(400);
      } else {
        return res.status(200).json(user.value);
      }
    }
  );

This is my user object:

{
    "_id": "616acc597ebda90ca6ffee21",
    "username": "test@test.com",
    "displayName": "Test",
    "devices": [
      {
        "serial": "865674036421275",
        "data": [
          {
            "timestamp": "2021-10-16T12:36:35.799Z"
          },
          {            
            "timestamp": "2021-10-16T12:41:33.633Z"
          },
          {           
            "timestamp": "2021-10-16T12:52:03.055Z"
          }
        ]
      },
      
    ],
    "hashedPassword": "R5vt3vY7vEvTHvhC7YGNOWuIjBUQGLqsd92QGE06tjU=",
    "salt": "6RS0OVIFboCkIEPHdZmTcQ==",
    "dateCreated": "2021-10-16T12:58:00.294Z"
  }

How will I check if serial is already in devices? Also, is it possible to throw an error if it already exists?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

There's an update operator called addToSet check it here

about 4 years ago · Juan Pablo Isaza Denunciar

0

Query

  • find to update only documents that dont have this serial
    (with eq/ne query operators, we can do arrays=value, if array has the value its true)
  • replace the "000" with the serial you want to add
  • push in the end
  • updateOne will return the result, and you can see if update is done or not, see your driver method details.

*it will be fast, and you dont need an index on devices.serial because it selects a specific document already with the _id match

Test code here

db.collection.update({
  "_id": ObjectId("616acc597ebda90ca6ffee21"),
  "devices.serial": {
    "$ne": "000"
  }
},
{
  "$push": {
    "devices": {
      "serial": "000",
      "data": []
    }
  }
})

We cant mix update operators with aggregate operators. $cond is aggregate operator, it cant be mixed with $push update operator. (aggregate $push is for group, cant add to an array)

We can do the update with update pipeline, but here a normal update is simpler.

about 4 years ago · Juan Pablo Isaza Denunciar

0

MongoDB Raw Query

db.userCollection.updateOne({
    _id: ObjectId('616acc597ebda90ca6ffee21'), // Object Id, make sure It is Object Id only else match just string version
    "devices.serial": {$ne: "SerialNeedToMatch" }
}, {
    $push: {
        "devices": {
            serial: '111' // or complete object 
        }
    }
})
about 4 years ago · Juan Pablo Isaza 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