使用 MongoDB Java 驱动程序版本 3 插入文档

Inserting Documents with MongoDB Java driver version 3


我们有一些应用程序使用 MongoDB 和 Java 驱动程序(2.x 版本)。在我们的应用程序中,我们使用 BasicDBObject 插入对象,例如:

BasicDBObject doc = new BasicDBObject("title", "MongoDB").
            append("description", "database").
            append("likes", 100);

查看 Java Driver 3.0 的文档,我发现使用了另一种方法:

Document doc = new Document("name", "MongoDB")
               .append("type", "database")
               .append("count", 1)
               .append("info", new Document("x", 203).append("y", 102));

BasicDBObject 是否已在 3.0 版中弃用? (希望不是,我们必须更改所有代码)

您可以在 3.0 API 中继续使用 DBObject。现有代码在遇到 3.0 驱动程序时应该(大部分)有效。切换到新的 APIs 有很多好处,但随着时间的推移,您可以在闲暇时做些事情。