Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!