Home > other >  MongoDB conditional update array elements
MongoDB conditional update array elements

Time:09-30

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
}

CodePudding user response:

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

  • Related