Dachstein
Dachstein

Reputation: 4282

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)
)

Upvotes: 4

Views: 1618

Answers (2)

Penchu
Penchu

Reputation: 31

You can try this:

Update update = new Update();

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

mongoOperations.findAndModify(
    Query.query(...),
    update
)

Upvotes: 3

NIKHIL NAGRALE
NIKHIL NAGRALE

Reputation: 87

You would want to do something like this :

Update update = new Update();
update.addToSet("answers", BasicDBObjectBuilder.start("$each", newAnswers).get());

mongoOperations.findAndModify(
    Query.query(...),
    update
)

Upvotes: 1

Related Questions