0

If I have an array inside of another array in MongoDB, how should I do to update a value in that array with mongoose? Right now the code looks like this, but nothing happens when it runs:

const newState = await MyModel.findOneAndUpdate(
      { code: code, 'array1.array2.identifier': identifier},
      { $set: { 'array1.array2.$.value': true } },
      { new: true }
);

In another place where I have only one array it works like a charm, and that codes looks like this:

const lobby = await Lobby.findOneAndUpdate(
      { code: code, 'array1.identifier': identifier},
      { $set: { 'array1.$.value': true } },
      { new: true }
);

So how do I get the first example where I have multiple arrays to work?

SomethingElse
  • 97
  • 1
  • 12
  • 2
    I think you should show some sample data. I hope you realize only one matched document will change with the $ operator. – Tushar Shahi Jul 08 '21 at 06:42
  • Yes, I only want one document to change, and this document contains both the arrays – SomethingElse Jul 08 '21 at 06:47
  • Kindly share your mongoose log – Ngodza Jul 08 '21 at 06:57
  • @SomethingElse look at this similar question [Updating a Nested Array with MongoDB](https://stackoverflow.com/questions/23577123/updating-a-nested-array-with-mongodb), i would suggest you to refer [arrayFilters](https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/) document as well. – turivishal Jul 08 '21 at 06:59

0 Answers0