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

138
Vistas
MongoDB conditional update array elements

I'm using Mongoose in a Node.js backend and I need to update a subset of elements of an array within a document based on a condition. I used to perform the operations using save(), like this:

const channel = await Channel.findById(id);
  channel.messages.forEach((i) =>
    i._id.toString() === messageId && i.views < channel.counter
      ? i.views++
      : null
  );
  await channel.save();

I'd like to change this code by using findByIdAndUpdate since it is only an increment and for my use case, there isn't the need of retrieving the document. Any suggestion on how I can perform the operation?

Of course, channel.messages is the array under discussion. views and counter are both of type Number.

EDIT - Example document:

{
    "_id": {
            "$oid": "61546b9c86a9fc19ac643924"
    },
    "counter": 0,
    "name": "#TEST",
    "messages": [{
        "views": 0,
        "_id": {
            "$oid": "61546bc386a9fc19ac64392e"
        },
        "body": "test",
        "sentDate": {
            "$date": "2021-09-29T13:36:03.092Z"
        }
    }, {
        "views": 0,
        "_id": {
            "$oid": "61546dc086a9fc19ac643934"
        },
        "body": "test",
        "sentDate": {
            "$date": "2021-09-29T13:44:32.382Z"
        }
    }],
    "date": {
        "$date": "2021-09-29T13:35:33.011Z"
    },
    "__v": 2
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try updateOne method if you don't want to retrieve document in result,

  • match both fields id and messageId conditions
  • check expression condition, $filter to iterate loop of messages array and check if messageId and views is less than counter then it will return result and $ne condition will check the result should not empty
  • $inc to increment the views by 1 if query matches using $ positional operator
messageId = mongoose.Types.ObjectId(messageId);
await Channel.updateOne(
  {
    _id: id,
    "messages._id": messageId,
    $expr: {
      $ne: [
        {
          $filter: {
            input: "$messages",
            cond: {
              $and: [
                { $eq: ["$$this._id", messageId] },
                { $lt: ["$$this.views", "$counter"] }
              ]
            }
          }
        },
        []
      ]
    }
  },
  { $inc: { "messages.$.views": 1 } }
)

Playground

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