findAndUpdate with nested array

How can I find and edit an object that is in offers.photos and search for it by id?

Hello, welcome to MongoDB Community

Try this:

db.collection.find({
  "offers.photos._id": ObjectId("ID_HERE")
});

If anyone will search it, this is the answer:

const result = await this.companyModel.updateOne(
        { 'offers._id': new Types.ObjectId(offerId) },
        { $set: { 'offers.$[].photos.$[x].order': 0 } },
        { arrayFilters: [ { 'x._id': new Types.ObjectId(photoId) } ] }
      );
1 Like