Morphia Mongodb Update in Embedded Array document fails

笑着哭i 提交于 2019-12-24 09:18:46

问题


I am new to Morphia and am trying to update a field in embedded document. This is the current structure

class A {

    List<B> BList;
}

class B {
    String field;
}

So My structure looks like the following in MongoDb :

{

    "_id" : ObjectId("5bab8be0032945f6e9f91d98"),
    "className" : "com.abc.A",
    "BList" : [ 
        {
            "B" : {
                "field" : "text"
                }
        }
}

Now I want to update B.field for all the matching queries.
I created the following UpdateOperations

 UpdateOperations updateOps = datastore.createUpdateOperations(A.class);

my filter query was fine say filter returning me all elements in A.

 updateOps.set("Blist.$[].B.field", "newtext");

when debugger reached this statement it shows org.mongodb.morphia.query.ValidationException: Could not resolve path 'BList.$[].B.field' against A`

I even added disableValidation But this doesnot run.

However if I ommit the positional operator it works fine but when on runnig datastore.update() it fails. throwing this error.

 Write failed with error code 28 and error message 'Cannot create field B.

Can Anyone suggest how to do this updation on second level in morphia ?

来源:https://stackoverflow.com/questions/52613735/morphia-mongodb-update-in-embedded-array-document-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!