【问题标题】:How to write query for update embedded document如何编写更新嵌入文档的查询
【发布时间】:2015-05-27 12:47:18
【问题描述】:

我是 Spring Data 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 ,如何为此编写查询?

【问题讨论】:

    标签: java mongodb spring-boot spring-data-mongodb


    【解决方案1】:

    您应该使用 $ 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() 之类的存储库中编写它,我对 spring data mongodb 完全陌生,提前谢谢
    • @PrabjotSingh 我对 spring 存储库不太满意,但是 this 可能会有所帮助
    【解决方案2】:

    来自@Query java 文档 org.springframework.data.mongodb.repository.Query

    直接在存储库上声明 finder 查询的注释 方法。这两个属性都允许使用占位符符号 ?0, ?1 等等。

    这里是你可以传递给注解的所有属性(下)

    从定义看来,您似乎只能读取、过滤特定字段、执行 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-21
      • 2022-11-30
      • 2013-06-12
      • 2012-08-22
      • 2020-11-19
      • 2012-08-26
      • 1970-01-01
      • 2012-05-11
      相关资源
      最近更新 更多