Morphia Mongodb Update in Embedded Array document

2019-08-10 00:37发布

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 ?

0条回答
登录 后发表回答