我有一个产品,这个产品有多个国家。所以我想删除mongo repository中的相应国家/地区。如何从spring数据mongodb存储库中删除。 请在下面找到我的JSON。
{ "_id" : ObjectId("565e0e1758da2b685eef727f"),
"offeringId" : "PMRS674913",
"offeringType" : "FEATURE",
"service" : "HBOGO",
"countries" : [
{
"country" : "OTT",
"isoCode" : "124",
"startDate" : "2015-01-01T00:00:00.000-05:00",
"endDate" : "2015-01-31T00:00:00.000-05:00"
},
{
"country" : "United States",
"isoCode" : " 456",
"startDate" : "2015-01-01T00:00:00.000-05:00",
"endDate" : "2015-01-31T0000:00.000-05:00"
}
]
}
答案 0 :(得分:0)
您可以使用以下查询:
db.collection.update(
{ "countries" : { $elemMatch : { "country" : "OTT" } } } ,
{ $pull : { "countries" : { "country" : "OTT" } } },
{ multi : true }
)
在Spring
中,您必须使用以下语法:
Query query = new Query();
query.addCriteria(Criteria.where("countries")
.elemMatch(Criteria.where("country").is("United States")));
Update update = new Update();
update.pull("countries", new BasicDBObject("country", "United States"));
mongoTemplate.updateMulti(query, update, collection);