Mongoose: Stages for updates

Created on 8 Oct 2019  路  3Comments  路  Source: Automattic/mongoose

I saw in official MongoDB docs that you can use aggregate on update. How can we do this in mongoose. Thanks.

https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/#stages-available-for-updates

Most helpful comment

@florianbepunkt yes it will. The caveat is that Mongoose will not cast properties aggregation pipelines, even in updateMany(). So if you choose to use aggregation update stages, you're on your own for casting values.

All 3 comments

Unfortunately this is a more significant undertaking than we thought. In order to work around the fact that Mongoose currently doesn't support pipelines in updateOne(), you need to use Model.collection.updateOne() to bypass Mongoose's casting. We're working on a fix and should have one out in the next 6 weeks :+1:

@vkarpov15 Will this fix also allow for aggregation updates in Model.updateMany? The mongodb driver allows for this. Thank you.

@florianbepunkt yes it will. The caveat is that Mongoose will not cast properties aggregation pipelines, even in updateMany(). So if you choose to use aggregation update stages, you're on your own for casting values.

Was this page helpful?
0 / 5 - 0 ratings