如何编写更新嵌入式文档的查询
How to write query for update embedded document
我是 spring 数据 mongodb 的新手,但我只是被困住了,如何使用 mongo 存储库为嵌入式文档编写基于 json 的查询。
我的数据库看起来像
"_id" : ObjectId("5565ad670cf25cbd975ab2d9"),
"_class" : "com.samepinch.domain.metadata.Metadata",
"preferenceType" : "shopping",
"subtypes" : [
{
"_id" : ObjectId("5565ad670cf25cbd975ab2d2"),
"subType" : "veg",
"preferencePoint" : 0
},
{
"_id" : null,
"subType" : "nonveg",
"preferencePoint" : 0
}
],
"createdDate" : ISODate("2015-05-27T11:41:27.357Z"),
"updatedDate" : ISODate("2015-05-27T11:41:27.357Z")
我想根据顶级文档 ID 更新子类型,我必须为 ID 为 5565ad670cf25cbd975ab2d2 的子类型更新 preferencePoint
,如何为此编写查询?
您应该将 $ projection 与 $elemMatch
查询一起使用,如下所示:
db.collectionName.update({"_id" : ObjectId("5565ad670cf25cbd975ab2d9"),
"subtypes":{"$elemMatch":{"_id":ObjectId("5565ad670cf25cbd975ab2d2")}}},
{"$set":{"subtypes.$.preferencePoint":1}})
及其等效的 java 代码为:
BasicDBObject eleMatch = new BasicDBObject();
eleMatch.put("_id", new ObjectId("5565ad670cf25cbd975ab2d2"));
BasicDBObject elemMatchQuery = new BasicDBObject();
elemMatchQuery.put("$elemMatch", eleMatch);
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId("5565ad670cf25cbd975ab2d9"));
query.put("subtypes", elemMatchQuery);
BasicDBObject set = new BasicDBObject();
set.put("subtypes.$.preferencePoint", 1);
BasicDBObject update = new BasicDBObject();
update.put("$set", set);
DBCollection dbcoll = mongoTemplate.getCollection("collectionName");
DBObject object = dbcoll.update(query, update);
来自@Query java 文档 org.springframework.data.mongodb.repository.Query
Annotation to declare finder queries directly on repository
methods. Both attributes allow using a placeholder notation of ?0, ?1
and so on.
以下是您可以传递给注释的所有属性(如下)
根据定义,您似乎只能读取、过滤特定字段、执行 count() 或删除与您的查询匹配的域对象。我没有看到任何关于更新的信息。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@QueryAnnotation
public @interface Query {
/**
* Takes a MongoDB JSON string to define the actual query to be executed. This one will take precendece over the
* method name then.
*
* @return
*/
String value() default "";
/**
* Defines the fields that should be returned for the given query. Note that only these fields will make it into the
* domain object returned.
*
* @return
*/
String fields() default "";
/**
* Returns whether the query defined should be executed as count projection.
*
* @since 1.3
* @return
*/
boolean count() default false;
/**
* Returns whether the query should delete matching documents.
*
* @since 1.5
* @return
*/
boolean delete() default false;
我是 spring 数据 mongodb 的新手,但我只是被困住了,如何使用 mongo 存储库为嵌入式文档编写基于 json 的查询。
我的数据库看起来像
"_id" : ObjectId("5565ad670cf25cbd975ab2d9"),
"_class" : "com.samepinch.domain.metadata.Metadata",
"preferenceType" : "shopping",
"subtypes" : [
{
"_id" : ObjectId("5565ad670cf25cbd975ab2d2"),
"subType" : "veg",
"preferencePoint" : 0
},
{
"_id" : null,
"subType" : "nonveg",
"preferencePoint" : 0
}
],
"createdDate" : ISODate("2015-05-27T11:41:27.357Z"),
"updatedDate" : ISODate("2015-05-27T11:41:27.357Z")
我想根据顶级文档 ID 更新子类型,我必须为 ID 为 5565ad670cf25cbd975ab2d2 的子类型更新 preferencePoint ,如何为此编写查询?
您应该将 $ projection 与 $elemMatch
查询一起使用,如下所示:
db.collectionName.update({"_id" : ObjectId("5565ad670cf25cbd975ab2d9"),
"subtypes":{"$elemMatch":{"_id":ObjectId("5565ad670cf25cbd975ab2d2")}}},
{"$set":{"subtypes.$.preferencePoint":1}})
及其等效的 java 代码为:
BasicDBObject eleMatch = new BasicDBObject();
eleMatch.put("_id", new ObjectId("5565ad670cf25cbd975ab2d2"));
BasicDBObject elemMatchQuery = new BasicDBObject();
elemMatchQuery.put("$elemMatch", eleMatch);
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId("5565ad670cf25cbd975ab2d9"));
query.put("subtypes", elemMatchQuery);
BasicDBObject set = new BasicDBObject();
set.put("subtypes.$.preferencePoint", 1);
BasicDBObject update = new BasicDBObject();
update.put("$set", set);
DBCollection dbcoll = mongoTemplate.getCollection("collectionName");
DBObject object = dbcoll.update(query, update);
来自@Query java 文档 org.springframework.data.mongodb.repository.Query
Annotation to declare finder queries directly on repository methods. Both attributes allow using a placeholder notation of ?0, ?1 and so on.
以下是您可以传递给注释的所有属性(如下)
根据定义,您似乎只能读取、过滤特定字段、执行 count() 或删除与您的查询匹配的域对象。我没有看到任何关于更新的信息。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@QueryAnnotation
public @interface Query {
/**
* Takes a MongoDB JSON string to define the actual query to be executed. This one will take precendece over the
* method name then.
*
* @return
*/
String value() default "";
/**
* Defines the fields that should be returned for the given query. Note that only these fields will make it into the
* domain object returned.
*
* @return
*/
String fields() default "";
/**
* Returns whether the query defined should be executed as count projection.
*
* @since 1.3
* @return
*/
boolean count() default false;
/**
* Returns whether the query should delete matching documents.
*
* @since 1.5
* @return
*/
boolean delete() default false;