You can simply use the below query to pull any string in the array of vegetables
object:
mongoTemplate.updateMulti(new Query(), new Update().pull("vegetables", "squash"), "stores");
In the above query, squash will be pulled after execution. For details, I have also added find query on stores
collections after and before the above query as :
List<Stores> storesList = mongoTemplate.find(new Query(), Stores.class);
for(Stores stores : storesList) {
System.out.println(stores.toString());
}
mongoTemplate.updateMulti(new Query(), new Update().pull("vegetables", "squash"), "stores");
List<Stores> afterModificationStoresList = mongoTemplate.find(new Query(), Stores.class);
for(Stores stores : afterModificationStoresList) {
System.out.println(stores.toString());
}
The output is as below :
2019-11-26 10:19:57.947 DEBUG 7321 --- [ main] o.s.data.mongodb.core.MongoTemplate : find using query: { } fields: Document{{}} for class: class sample.data.mongo.models.Stores in collection: stores
Stores{id='1.0', fruits=[apples, pears, oranges, grapes, bananas], vegetables=[celery, squash]}
Stores{id='2.0', fruits=[plums, kiwis, oranges, bananas, apples], vegetables=[broccoli, zucchini, onions]}
2019-11-26 10:19:57.975 DEBUG 7321 --- [ main] o.s.data.mongodb.core.MongoTemplate : Calling update using query: { } and update: { "$pull" : { "vegetables" : "squash" } } in collection: stores
2019-11-26 10:19:57.985 DEBUG 7321 --- [ main] o.s.data.mongodb.core.MongoTemplate : find using query: { } fields: Document{{}} for class: class sample.data.mongo.models.Stores in collection: stores
Stores{id='1.0', fruits=[apples, pears, oranges, grapes, bananas], vegetables=[celery]}
Stores{id='2.0', fruits=[plums, kiwis, oranges, bananas, apples], vegetables=[broccoli, zucchini, onions]}
For code details, kinldy visit by Github repo: https://github.com/krishnaiitd/learningJava/blob/master/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/main/Application.java#L126