从 java MongoDb 驱动程序 2.13 到 3.3 时文档与 DBObject 混淆

Document vs. DBObject confusion while moving from java MongoDb driver 2.13 to 3.3

我只是将我的项目从旧的 2.13 驱动程序更改为新的 3.3 java 驱动程序。现在鉴于 Document 无论如何都会替换 DBObject,我想我会替换它。一切都像一个魅力......除了(旧)数据库不接受我试图扔给它的文件。

insertOne 不应该接受 org.bson.Document 就好了,因为它以前接受 DBObject 吗?

我的文档是这样构建的:

static synchronized Document CommentsToDocument(Comment comment) {
    return new Document().append("source", comment.getSource()).append("date", comment.getDateTime())
            .append("author", comment.getAuthor()).append("thread", comment.getThreadID())
            .append("commentID", comment.getCommentID()).append("title", comment.getTitle())
            .append("comment", comment.getComment());
}

static synchronized Document UrlsToDocument(String url, int counter) {
    return new Document("url", url).append("count", counter);
}

然后将这些文档添加到双端队列中。并且应该这样插入:

static synchronized void writeToDb(String col, ConcurrentLinkedDeque<Document> comments) {
    MongoCollection<Document> collection = database.getCollection(col);
    for (Document o : comments) {
        collection.insertOne(o);
    }
}

是的,我知道,writeToDb 相当不优雅,我会想出更好的方法。 ;)

现在我只面临一个问题,接受相同组成的 BasiDBObjects 的数据库不接受文档。

对象是否在内部存储为 BasicDBObjects 以便我必须先解析文档?或者有没有办法让数据库按原样接受文档,数据库在内部解析它们?

或者我是否必须从旧数据库中读取所有对象,创建一个新对象,并在重新解析后将它们写回? o.O

感谢您的建议。

PS:我已经通读了 CRUD Operations info,但找不到它不接受我的文档对象的任何原因。我可能没有经验才能看到显而易见的东西,所以是的。 ;)

PPS: 有趣的是我可以很好地阅读文档。我发出的文件和试图进入的文件之间的唯一区别是缺少 _id ...这应该不是问题。我还研究了索引问题的可能性,但是 url 集合仅在 url(当然还有 _id)上唯一索引,并且应该接受新的(不同的)条目。

找到解决方案。显然,代码完全不同部分的一些 bool 标志被重置了。你关于例外的问题让我走上了正确的道路,因为没有。这意味着它根本不是数据库问题。数据库正确地处理了索引中已有的所有文档。