YellowPillow
YellowPillow

Reputation: 4270

Mongoose findByIdAndUpdate nested not updating object

I was wondering why:

const props = {
    excluded: true
}
const image = await Image.findByIdAndUpdate(
    imageId, {
        $set: 'image.ingest': props
    }, {
        new: true
    }
)

Doesn't update the image document with the excluded field whereas:

const props = {
    excluded: true
}
const image = await Image.findById(imageId);

image.ingest = {
    ...image.ingest,
    ...props
};

await image.save();

does?

Upvotes: 0

Views: 280

Answers (1)

J.F.
J.F.

Reputation: 15187

Your problem is you forgot to use { } after $set. Check this example.

Also, checked using mongoose this query and works fine:

var updated = await model.findByIdAndUpdate(id,
{
  "$set":{
    "image.ingest":"new value"
  }
},{
  "new":true
})

Upvotes: 1

Related Questions