Reputation: 141
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.
Upvotes: 1
Views: 560
Reputation: 13103
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");
Upvotes: 1