简体   繁体   English

如何编写查询以更新嵌入式文档

[英]How to write query for update embedded document

I new to spring data mongodb,but i am just stuck with,how to write json based query for embedded documents using mongo repository. 我是Spring Data MongoDB的新手,但我只是坚持使用如何使用mongo存储库为嵌入式文档编写基于json的查询。

My database looks like 我的数据库看起来像

"_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")

I want to update subtypes based upon top level document id,i have to update preferencePoint for subtype having id 5565ad670cf25cbd975ab2d2 ,how to write query for this ? 我想根据顶级文档ID更新子类型,我必须更新ID为5565ad670cf25cbd975ab2d2的子类型的preferencePoint,如何为此编写查询?

You should use $ projection with $elemMatch query look like : 您应该将$ projection$elemMatch查询一起使用,如下所示:

db.collectionName.update({"_id" : ObjectId("5565ad670cf25cbd975ab2d9"),
"subtypes":{"$elemMatch":{"_id":ObjectId("5565ad670cf25cbd975ab2d2")}}},
{"$set":{"subtypes.$.preferencePoint":1}})

And its equivalent java code as : 及其等效的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);

from the @Query java doc org.springframework.data. 来自@Query java doc org.springframework.data。 mongodb .repository. 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. 这两个属性均允许使用占位符符号?0,?1等。

Here are all the attributes you can pass to the annotation (below) 这是您可以传递给注释的所有属性(如下)

From the definition it seems you can only Read, filter for specific fields, perform count(), or delete the domain objects that match your query. 从定义看来,您只能读取,过滤特定字段,执行count()或删除与查询匹配的域对象。 I don't see anything regarding updates. 我没有任何关于更新的信息。

@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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM