Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Mongo: How to use the $each modifier?

How to use the $each modifier in the following findAndModify query of a MongoTemplate?

I have tried to find it in the Spring Docs, but nothing.

answers is the key in Mongo which holds an array.

new answers is an array which items should be added to the answers in Mongo.

mongoOperations.findAndModify(
    Query.query(...),
    Update().push("answers", newAnswers)
)
like image 769
Dachstein Avatar asked Oct 28 '25 14:10

Dachstein


1 Answers

You can try this:

Update update = new Update();

update.push("answers").each(newAnswers);

mongoOperations.findAndModify(
    Query.query(...),
    update
)
like image 195
Penchu Avatar answered Oct 31 '25 04:10

Penchu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!