美文网首页
MongoDB with Morphia's Details

MongoDB with Morphia's Details

作者: f923bd1a20e9 | 来源:发表于2016-12-06 09:29 被阅读0次

    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:

    相关文章

      网友评论

          本文标题:MongoDB with Morphia's Details

          本文链接:https://www.haomeiwen.com/subject/ldolmttx.html