Sameesh
Sameesh

Reputation: 353

MongoTemplate pull query

I am trying to remove one Item from an array of an embedded field in mongoDb. Array is type of string like the below one.

{
   _id: 1,
   fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
   vegetables: [ "carrots", "celery", "squash", "carrots" ]
}
{
   _id: 2,
   fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
   vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}

I just wanted to remove the carrots from the embedded array vegetables. using mongo shell, the below query works

db.stores.update(
    { },
    { $pull: { vegetables: "carrots" },
    { multi: true }
)

Now I needed to perform this using mongoTemplate in spring, I tried with the below answer but it wont remove the element MongoTemplate pull subdocument. Can anyone suggest How I to achieve this using mongoTemplate in spring project

Upvotes: 0

Views: 1029

Answers (1)

krishna Prasad
krishna Prasad

Reputation: 3812

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

Upvotes: 1

Related Questions