MongoDB update using Java 3 driver

前端 未结 3 1303
名媛妹妹
名媛妹妹 2020-12-30 03:51

I\'m switching to the MongoDB Java driver version 3. I cannot figure out how to perform an update of a Document. For example, I want to change the \"age\" of an user:

<
相关标签:
3条回答
  • 2020-12-30 04:30

    Use:

    coll.updateOne(eq("name", "frank"), new Document("$set", new Document("age", 33)));
    

    for updating the first Document found. For multiple updates:

    coll.updateMany(eq("name", "frank"), new Document("$set", new Document("age", 33)));
    

    On this link, you can fine a quick reference to MongoDB Java 3 Driver

    0 讨论(0)
  • 2020-12-30 04:30

    You can try this

    coll.findOneAndReplace(doc1, doc2);
    
    0 讨论(0)
  • 2020-12-30 04:38

    in Mongodb Java driver 3.0 , when you update a document, you can call the coll.replaceOne method to replace document, or call the coll.updateOne / coll.updateMany method to update document(s) by using $set/$setOnInsert/etc operators.

    in your case, you can try:

    coll.updateOne(eq("name", "frank"), new Document("$set", new Document("age", 33)));
    coll.replaceOne(eq("name", "frank"), new Document("age", 33));
    
    0 讨论(0)
提交回复
热议问题