How to update value of specific embedded document, inside an array, of a specific document in MongoDB?

后端 未结 4 2034
孤城傲影
孤城傲影 2020-12-03 01:45

I have the following structure in my document:

{
  _id : ObjectId(\"43jh4j343j4j\"), 
  array : [
            { 
              _arrayId : ObjectId(\"dsd87ds         


        
4条回答
  •  星月不相逢
    2020-12-03 02:35

    Here is RameshVel's solution translated to java:

        DB db = conn.getDB( "yourDB" ); 
        DBCollection coll = db.getCollection( "yourCollection" );
    
        ObjectId _id = new ObjectId("4e71b07ff391f2b283be2f95");
        ObjectId arrayId = new ObjectId("4e639a918dca838d4575979c");
    
        BasicDBObject query = new BasicDBObject();
        query.put("_id", _id);
        query.put("array._arrayId", arrayId);
    
        BasicDBObject data = new BasicDBObject();
        data.put("array.$.someField", "updated");
    
        BasicDBObject command = new BasicDBObject();
        command.put("$set", data);
    
        coll.update(query, command);
    

提交回复
热议问题