0

I try to update an array of object using MongoDB.

Exemple of object :

    [
      {
        _id: 'ID',
        name: 'hello',
        org: 'world',
        seenBy: [
          {
            _id: 'ID',
            seenAt: 'DATE'
          },
          ...
        ],
        ...
      }
    ]

I would like update the Date on seenBy.seenAt. For this, i try this Mongo update :

    await Task.updateOne(
      {
        _id: ctx.task._id,
        'seenBy._id': ctx.me._id
      },
      {
        $set: {
          "seenBy.$.seenAt": new Date()
        }
      },
    );

But it don't work, and i'm not really understand why. I target the correct item by checking the _id and the seenBy._id, but the new value is never add.

Any one know if i need other operation ? Maybe i'm wrong approch :D

Thank you community !

Stéphane R.
  • 1,386
  • 3
  • 19
  • 37
  • 1
    you have to use [arrayFitlers](https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/), there are lots of questions in SO. – turivishal Aug 12 '20 at 14:44
  • Does this answer your question? [How to update objects in array in Mongo](https://stackoverflow.com/questions/20668875/how-to-update-objects-in-array-in-mongo) – Joe Aug 13 '20 at 02:25

0 Answers0