Himanshu Rahi
Himanshu Rahi

Reputation: 301

How to update all array value in document at once

Actually am trying to update an array in my document which have read property is set to false here what i have done so far.

const user = await User.update(
    {
      _id: req.userData.id
    },
    {
      $set: {
        "notifications.$[elem].read": true
      },
      arrayFilters: [
        {
          "elem.read": false
        }
      ], multi : true
    }
  );

but am getting an error

(node:12572) UnhandledPromiseRejectionWarning: MongoError: No array filter found for identifier 'elem' in path 'notifications.$[elem].read'

Upvotes: 0

Views: 28

Answers (1)

Valijon
Valijon

Reputation: 13103

arrayFilters should be defined in options.

Try below code:

const user = await User.update(
  {
    _id: req.userData.id
  },
  {
    $set: {
      "notifications.$[elem].read": true
    }
  },
  {
    arrayFilters: [
      {
        "elem.read": false
      }
    ], 
    multi : true
  }
);

Upvotes: 1

Related Questions