MongoDB update using Java 3 driver

前端 未结 3 1334
名媛妹妹
名媛妹妹 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: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));
    

提交回复
热议问题