Java Spring MongoDB标准更新

Java Spring MongoDB标准更新,java,mongodb,spring-mongo,Java,Mongodb,Spring Mongo,如何编写查询以更新“contacts.collection.value.\u class=subclass 2”并使用Spring Mongodb条件将值设置为xyz { "_class" : "MyClass", "_id" : ObjectId( "51ecc95503644e4489bb742e" ), "contacts" : [ { "property1" : "Value1", "property2" : "Value2", "collect

如何编写查询以更新“contacts.collection.value.\u class=subclass 2”并使用Spring Mongodb条件将值设置为xyz

{ "_class" : "MyClass",
  "_id" : ObjectId( "51ecc95503644e4489bb742e" ),
  "contacts" : [ 
    { "property1" : "Value1",
      "property2" : "Value2",
      "collection" : [ 
        { "value" : "1",
          "_class" : "SubClass1" }, 
        { "value" : "2",
          "_class" : "SubClass2" }, 
        { "value" : "2",
          "_class" : "SubClass3" }, 
我试图用SpringDataMongo标准类来实现这一点

到目前为止,我得到了这个,但它不工作

            query = new Query(Criteria.where("_id").is(myClassId)
                    .and("contacts.collection.value").is("2")
                    .and("contacts.collection._class").is("SubClass2"));
            update.set("contacts.collection.$.value", "3");
            mongoTemplate.updateFirst(query, update, MyClass.class);
我得到了这个错误

java.lang.IllegalArgumentException:在…上找不到属性值

注:

  • “联系人”集合是一些界面的列表
  • 我清理了变量名和类名
  • 如果您不知道如何使用标准,请给我java代码
  • 谢谢

    添加stacktrace

    这是堆栈跟踪

    java.lang.IllegalArgumentException: No property xyz found on com.blah.SomeInterface!
            at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:225) ~[spring-data-commons-1.5.1.RELEASE.jar:na]
            at org.springframework.data.mongodb.core.convert.QueryMapper.getPath(QueryMapper.java:202) ~[spring-data-mongodb-1.2.1.RELEASE.jar:na]
            at org.springframework.data.mongodb.core.convert.QueryMapper.getTargetProperty(QueryMapper.java:190) ~[spring-data-mongodb-1.2.1.RELEASE.jar:na]
            at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:86) ~[spring-data-mongodb-1.2.1.RELEASE.jar:na]
            at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:925) ~[spring-data-mongodb-1.2.1.RELEASE.jar:na]
            at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:920) ~[spring-data-mongodb-1.2.1.RELEASE.jar:na]
            at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:388) ~[spring-data-mongodb-1.2.1.RELEASE.jar:na]
            at org.springframework.data.mongodb.core.MongoTemplate.doUpdate(MongoTemplate.java:920) ~[spring-data-mongodb-1.2.1.RELEASE.jar:na]
            at org.springframework.data.mongodb.core.MongoTemplate.updateFirst(MongoTemplate.java:902) ~[spring-data-mongodb-1.2.1.RELEASE.jar:na]
    
    根据Jayz的回答,我用RoboMongo编写了更新,它可以更新查询结果

    db.myDB.update(
    {
    "_id":ObjectId("51ecc95503644e4489bb742e"),
    "contacts.collection._class": "SubClass1",
    "contacts.collection.value": "1",
    },
    { 
        $set: { 'contacts.0.collection.$.value': '3' } 
    },
    { getLastError: 1 });
    

    但是,当我尝试使用mongoTemplate执行此操作时,没有更新任何内容。

    查询将为您获取与查询条件匹配的集合数组的索引。对于数据结构,您需要两个数组索引,一个用于联系人,一个用于收集,这是不可能的(据我所知)。您需要知道联系人数组项或集合数组项的位置。假设您知道contacts数组项的位置。如果是第0位,以下是您的完整查询:

    query = new Query(new Criteria().andOperator(
            Criteria.where("_id").is(myClassId),
            Criteria.where("contacts.collection.value").is("2"),
            Criteria.where("contacts.collection._class").is("SubClass2"));
            update.set("contacts.0.collection.$.value", "3");
            mongoTemplate.updateFirst(query, update, MyClass.class);
    

    查询将获得与查询条件匹配的集合数组的索引。对于数据结构,您需要两个数组索引,一个用于联系人,一个用于收集,这是不可能的(据我所知)。您需要知道联系人数组项或集合数组项的位置。假设您知道contacts数组项的位置。如果是第0位,以下是您的完整查询:

    query = new Query(new Criteria().andOperator(
            Criteria.where("_id").is(myClassId),
            Criteria.where("contacts.collection.value").is("2"),
            Criteria.where("contacts.collection._class").is("SubClass2"));
            update.set("contacts.0.collection.$.value", "3");
            mongoTemplate.updateFirst(query, update, MyClass.class);
    

    筛选的位置运算符$标识与更新操作的arrayFilters条件匹配的数组元素

    query=新查询(新条件().andOperator)( 标准,其中(“_id”)为(myClassId), 标准。其中(“contacts.collection.value”)为(“2”), 标准。其中(“联系人收集”为(“第2子类”); update.set(“contacts.$.collection.$.value”,newValueToUpdate在相应位置); updateFirst(查询、更新、MyClass.class)


    筛选的位置运算符$标识与更新操作的arrayFilters条件匹配的数组元素

    query=新查询(新条件().andOperator)( 标准,其中(“_id”)为(myClassId), 标准。其中(“contacts.collection.value”)为(“2”), 标准。其中(“联系人收集”为(“第2子类”); update.set(“contacts.$.collection.$.value”,newValueToUpdate在相应位置); updateFirst(查询、更新、MyClass.class)


    您可以发布整个stacktrace吗?您可以发布整个stacktrace吗?谢谢@Jayz这是正确的查询,但更新不起作用联系人是一个集合也集合是一个集合。那么我如何指定另一个集合的集合内的值呢?下面不起作用-contacts.collection.$.value也已尝试-contacts.0.collection.$.value无效。很遗憾,它没有更新任何内容。我添加了WriteResult以查看结果getN()为0,而result.getLastError().getErrorMessage()为空。是否检查了要更新的contacts集合的数组索引?我创建了一个测试类,创建了整个文档,并选中了(在mongo shell中)必须更新集合集合的联系人的数组索引为0,然后使用测试类运行查询,结果正常。子文档(集合集合)中的值已更新。对于您的情况,假设您的文档与此处给定的文档完全相同,数组索引为1,请使用:update.set(“联系人.1.集合.$值”,“3”);我已经能够通过在两个集合上使用精确索引来解决此问题。我本来希望我可以在不提供此类信息的情况下编写更新查询,但现在似乎不可能。再次感谢您的回答。我相信您可以使用正确的条件避免在一个集合上传递精确索引。请标记如果有助于您解决问题,则答案正确。谢谢@Jayz这是正确的查询,但更新不起作用。联系人是一个集合,集合也是一个集合。那么如何指定另一个集合的集合内的值?以下不起作用-contacts.collection.$.value也已尝试-contacts.0.collecti在上,$.value不起作用。不幸的是,它不更新任何内容。我添加了WriteResult以查看结果getN()为0,而result.getLastError().getErrorMessage()为空。是否检查了要更新的联系人集合的数组索引?我创建了一个测试类,创建了整个文档,并进行了检查(在mongo shell中)必须更新集合集合的联系人的数组索引为0,然后使用测试类运行查询,查询成功。子文档(集合集合)中的值已更新。对于您的情况,假设您的文档与此处给定的文档完全相同,数组索引为1,请使用:update.set(“联系人。1.集合。$值,“3”);我已经能够通过在两个集合上使用精确索引来解决此问题。我本来希望我可以在不提供此类信息的情况下编写更新查询,但现在似乎不可能。再次感谢您的回答。我相信您可以使用正确的条件避免在一个集合上传递精确索引。请标记如果有助于您解决问题,则答案是正确的。