简体   繁体   English

如何更新 MongoDB 中特定文档的数组内特定嵌入文档的值?

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

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 (eg _arrayId : ObjectId("dsd87dsa9d87s9d7") ; and only for this document (eg _id : ObjectId("43jh4j343j4j") ) and no other.我想更新someFieldsomeField2但仅针对数组中的一项,即与_arrayId匹配的一项(例如_arrayId : ObjectId("dsd87dsa9d87s9d7") ;并且仅针对此文档(例如_id : ObjectId("43jh4j343j4j") )没有其他。

The arrayIds are not unique to the document that's why I need it to be for a specific document. arrayIds不是文档所独有的,这就是为什么我需要它用于特定文档。 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.如果我想为数组中存在的每个文档更新该值,我可以使用$ positional operator ,但这不是我想要的。

I am trying to accomplish this in but a command line solution would work as well.我正在尝试用完成此操作,但命令行解决方案也可以使用。

Here is RameshVel's solution translated to :这是 RameshVel 的解决方案翻译成

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

You could still use $ positional operator to accomplish this.您仍然可以使用$ positional运算符来完成此操作。 But you need to specify the objectid of the parent doc along with the _arrayid filter.但是您需要指定父文档的 objectid 以及 _arrayid 过滤器。 The below command line query works fine下面的命令行查询工作正常

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

...and this is how to do it with mongo-driver version >= 3.1 (mine is 3.2.2): ...这是如何使用 mongo-driver 版本 >= 3.1(我的是 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);

Seeing as none of the answers actually explain how to do this a) in Java and b) for multiple fields in a nested array item, here is the solution for mongo-java-driver 3.12.3.看到没有一个答案实际上解释了如何在 Java 中执行此操作 a) 和 b) 嵌套数组项中的多个字段,这里是 mongo-java-driver 3.12.3 的解决方案。

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.Document;
import org.bson.types.ObjectId;

MongoClient mongoClient = MongoClients.create(...);
MongoDatabase db = mongoClient.getDatabase("testDb");
MongoCollection<Document> collection = db.getCollection("testCollection");
collection.updateOne(
    Filters.and(
            Filters.eq("_id", new ObjectId("43jh4j343j4j")),
            Filters.eq("array._arrayId", new ObjectId("dsd87dsa9d87s9d7"))
    ),
    Updates.combine(
            Updates.set("array.$.someField", "new value 1"),
            Updates.set("array.$.someField2", "new value 2")
    )
);

This thread has helped me towards the right solution, but I had to do more research for the full solution, so hoping that someone else will benefit from my answer too.这个线程帮助我找到了正确的解决方案,但我必须为完整的解决方案做更多的研究,所以希望其他人也能从我的回答中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM