java - How to retrieve and remove embedded document spring data mongodb -
there stuck, how remove embedded document in mongodb. using spring data mongodb criteria, doing following:
// database "_id" : objectid("55683d51e4b0b6050c5b0db7"), "_class" : "com.samepinch.domain.metadata.metadata", "preferencetype" : "shopping", "subtypes" : [ { "_id" : objectid("55683d51e4b0b6050c5b0db6"), "leftvalue" : "veg", "rightvalue" : "non_veg", "preferencepoint" : 0 } ], "createddate" : isodate("2015-05-29t10:20:01.610z"), "updateddate" : isodate("2015-05-29t10:20:01.610z") // query mongotemplate.updatemulti(new query(), new update().pull("subtypes", query.query(criteria.where("subtypes._id").is(new objectid("55683d51e4b0b6050c5b0db6"))),metadata.class);
what doing wrong? in advance!
subtypes
in nested objects should first pass in $elemmatch matched first matching array elements of given conditions. update query :
db.updatemulti.update({"subtypes":{"$elemmatch":{"_id":objectid("55683d51e4b0b6050c5b0db6")}}}, {"$pull":{"subtypes":{"_id":objectid("55683d51e4b0b6050c5b0db6")}}})
this query pull exact matching array elements subtypes
array .
and of spring elemmatch ( not expertise in spring mongo ) converted query in spring format below :
mongotemplate.updatemulti(new query( where("subtypes").elemmatch(where("_id").is(ew objectid("55683d51e4b0b6050c5b0db6"))).pull( pull("subtypes", query.query(criteria.where("_id").is(new objectid("55683d51e4b0b6050c5b0db6"))),metadata.class ));
this above spring query not tested hope convert mongo update query in spring mongo query format.
Comments
Post a Comment