且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java中使用ObjectID更新MongoDB中的文档

更新时间:2023-01-16 08:06:16

您当前正在尝试基于字符串而不是ObjectId进行更新.

You're currently trying to update based on a string, not an ObjectId.

在构建查询时,请确保从字符串初始化新的ObjectId:

Make sure to initialise a new ObjectId from the string when building your query:

mongoDB.getCollection("restaurants").updateOne(
    new BasicDBObject("_id", new ObjectId("56110fe1f882142d842b2a63")),
    new BasicDBObject("$set", new BasicDBObject("zipcode", "10462"))
);