1

I have a collection named pub. Each document contains a array of strings named cocktails, So that :

{"id" : "0", "name" : "My Beautiful Pub", "cocktails" : ["B52", "SexOnTheBeach", "Negroni"]}

Using Spring mongodb, I would delete every "SexOnTheBeach", so that :

{"id" : "0", "name" : "My Beautiful Pub", "cocktails" : ["B52", "Negroni"]}

... every pub document in my "pub" collection cannot contain the "SexOnTheBeach".

I think I should use the new Update().pull() method, but every update doesn't modify my db.

This mongodb query works perfectly, but I don't know how to translate it!

db.pub.update(
{ },
{ $pull: {  cocktails: "SexOnTheBeach" } },
{ multi: true })

Ty in advance.

1 Answer 1

1

In your @Service class, add:

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.Query;

@Autowired
private MongoTemplate mongoTemplate;

...

mongoTemplate.updateMulti(new Query(), new Update().pull("cocktails", "SexOnTheBeach"), "pub");
Sign up to request clarification or add additional context in comments.

1 Comment

TY, perfect solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.