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

自古美人都是妖i 提交于 2019-11-26 20:02:04

问题


I have the following structure in my document:

{
  _id : ObjectId("43jh4j343j4j"), 
  array : [
            { 
              _arrayId : ObjectId("dsd87dsa9d87s9d7"),
              someField : "something",
              someField2 : "something2"
            },
            { 
              _arrayId : ObjectId("sds9a0d9da0d9sa0"),
              someField : "somethingElse",
              someField2 : "somethingElse2"
            }
   ]
 }

I want to update someField and someField2 but only for one of the items in the array, the one that matches _arrayId (e.g. _arrayId : ObjectId("dsd87dsa9d87s9d7"); and only for this document (e.g. _id : ObjectId("43jh4j343j4j") ) and no other.

The arrayIds are not unique to the document that's why I need it to be for a specific document. I could use the $ positional operator if I wanted to update that value within the array for every document it exists in, but that's not what I want.

I am trying to accomplish this in java but a command line solution would work as well.


回答1:


You could still use $ positional operator to accomplish this. But you need to specify the objectid of the parent doc along with the _arrayid filter. The below command line query works fine

db.so.update({_id:ObjectId("4e719eb07f1d878c5cf7333c"),
              "array._arrayId":ObjectId("dsd87dsa9d87s9d7")},
              {$set:{"array.$.someField":"updated"}})



回答2:


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);



回答3:


...and this is how to do it with mongo-driver version >= 3.1 (mine is 3.2.2):

final MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURIString));
final MongoDatabase blogDatabase = mongoClient.getDatabase("yourDB");
MongoCollection<Document> postsCollection = blogDatabase.getCollection("yourCollection");

ObjectId _id = new ObjectId("4e71b07ff391f2b283be2f95");
ObjectId arrayId = new ObjectId("4e639a918dca838d4575979c");

Bson filter = Filters.and(Filters.eq( "_id", id ), Filters.eq("array._arrayId", arrayId));
Bson setUpdate = Updates.set("array.$.someField", "updated");
postsCollection.updateOne(postFilter, setUpdate);


来源:https://stackoverflow.com/questions/7425993/how-to-update-value-of-specific-embedded-document-inside-an-array-of-a-specifi

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