MongoTemplate pull subdocument
Asked Answered
H

3

15

I need to pull a subdocument in MongoTemplate but cannot figure out how to do it.

My saved document is:

{
    "_id" : "FooUser",
    "_class" : "com.domain.User",
    "tests" : [ 
        {
            "variant" : {
                "_id" : "C",
                "probability" : "0.5"
            },
            "experiment" : {
                "$ref" : "experiment",
                "$id" : "MyExperiment2"
            }
        }, 
        {
            "variant" : {
                "_id" : "B",
                "probability" : "0.5"
            },
            "experiment" : {
                "$ref" : "experiment",
                "$id" : "MyExperiment1"
            }
        }
    ]
}

I need to remove only the test that has MyExperiment1. Executing the following command works:

db.user.update( {}, {$pull: { "tests":{"experiment.$id":"MyExperiment1"}}}, {multi: true} )

How should I write this using Spring MongoTemplate?

I have tried the following, but does not work:

this.mongoTemplate.updateMulti(new Query(), new Update().pull("tests", "{\"experiment.$id\":\"MyExperiment1\"}"), "user");

Thanks.

Haskins answered 26/11, 2014 at 15:26 Comment(0)
H
24

It seems this works:

this.mongoTemplate.updateMulti(new Query(),
        new Update().pull("tests", Query.query(Criteria.where("experiment.$id").is("MyExperiment1"))), USERS_COLLECTION_NAME);
Haskins answered 26/11, 2014 at 15:32 Comment(1)
cid="idvalue" this.mongoTemplate.updateMulti(new Query(), new Update().pull("tests", Query.query(Criteria.where("experiment.$id").is(cid))), USERS_COLLECTION_NAME); It is matching result but not removing the element. how to match string id value ObjectId in pull opration.Hofmann
J
0

Another solution which worked fine for me is the use of a BasicDBObject into the pull method's value param. See How to code with Spring data MongoDB for db.test.update({name:'abc'}, {$pull: {'child': {'age':10}}})

Jovian answered 26/7, 2016 at 9:40 Comment(0)
M
0

That's my solution:

Pulling child-field

  public Mono<Project> DeleteCritTemplArray(String id, String country) {

    Query query = new Query();
    query.addCriteria(Criteria.where("_id")
                              .is(id));

    Update update = new Update();
    update.pull("countryList", country);

    return template
         // findAndModify:
         // Find/modify/get the "new object" from a single operation.
         .findAndModify(
              query, update,
              new FindAndModifyOptions().returnNew(true), Project.class
                       );

  }

Pulling child-Object

 public Mono<ProjectChild> DeleteCritTemplChild(String id, String idch) {

    Query query = new Query();
    query.addCriteria(Criteria
                           .where("_id")
                           .is(id)
                           .and("tasks._id")
                           .is(idch)
                     );

    Update update = new Update();
    update.set("tasks.$", null);

    return template
         // findAndModify:
         // Find/modify/get the "new object" from a single operation.
         .findAndModify(
              query, update,
              new FindAndModifyOptions().returnNew(true), ProjectChild.class
                       );
  }
Metrorrhagia answered 23/12, 2021 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.