Morphia Grammar
Update arrays of embedded documents
We can use "key.index.key" to locate the values in the array which we intend to modify.
Query query = BallMongoUtils.datastore.createQuery(Ball.class).disableValidation().
field(Mapper.ID_KEY).equal(newObjectId("583646cacd8ba00e54188f73"));
UpdateOperations updateOperations =
BallMongoUtils.datastore.createUpdateOperations(Ball.class)
.disableValidation()
.set("newField.0.num","10");
//BallMongoUtils.datastore.createUpdateOperations(Ball.class)
// .disableValidation()
// .set("newField."+1+".num","11");
BallMongoUtils.datastore.update(query,updateOperations);
The document below
// colleciton: Ball
{
"_id" : ObjectId("583646cacd8ba00e54188f73"),
"className" : "com.me.entity.Ball",
"name" : "FOUR",
"radius" : 12.45,
"colour" : "6e10f4",
"number" : "10",
"status" : "disable",
"newField" : [
{
"num" : "10",
"name" : "aaa"
},
{
"num" : "11",
"name" : "bbb"
}
}
}
Update: set a list of embedded documents
First, a List Object was essential .Got it from the Query:
Then set this list into another document:
网友评论