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

后端 未结 4 2020
孤城傲影
孤城傲影 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:26

    ...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);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-03 02:35

    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.

    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.

    0 讨论(0)
  • 2020-12-03 02:41

    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"}})
    
    0 讨论(0)
提交回复
热议问题