[英]How to query with greaterThan for an embedded document using Spring Data MongoDB Repository?
[英]How to retrieve and remove embedded document spring data mongodb
我被困在那里,如何删除mongodb中的嵌入式文档。 我正在使用spring数据mongodb标准,我这样做如下:
// 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);
我做错了什么? 提前致谢!
subtypes
位于嵌套对象中,因此您应首先在$ elemMatch中将其传递给匹配给定条件的第一个匹配数组元素。 将查询更新为:
db.updateMulti.update({"subtypes":{"$elemMatch":{"_id":ObjectId("55683d51e4b0b6050c5b0db6")}}},
{"$pull":{"subtypes":{"_id":ObjectId("55683d51e4b0b6050c5b0db6")}}})
此查询从subtypes
array中提取完全匹配的数组元素。
在这个春季elemMatch的帮助下( 在spring mongo中没有那么多的专业知识 ),我将该查询转换为spring格式,如下所示:
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
));
上面的这个Spring查询未经测试,希望您将Spring Mongo查询格式转换为mongo更新查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.