Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda