MongoDB 是否确保在成功保存响应后,数据将立即可搜索

Does MongoDB ensures that after a successful save response, the data will be instantly searchable

我正在做一个 java/kotlin 项目,应该可以立即搜索刚刚保存的对象。就像 sql 数据库 (PostgreSQL) 中的同步保存一样。我读到 MongoDB Java 驱动程序提供与 MongoDB 的同步和异步交互。

但是 MongoDB 是否确保在成功保存响应后,数据将立即可搜索?也许它有一个内部队列,用于像 Elasticsearch 和 Couchbase 中那样尚未真正保存的数据?

换句话说,是否会抛出异常?例如在重负载下。我们假设 someId = object.some_id.

fun saveObjectInMongo(collection: MongoCollection<Document>, someId: String, object: String) {
    collection.insertOne(Document.parse(object))
    if (collection.find(eq("some_id", someId)).first().isNullOrEmpty()) {
        throw RuntimeException("Can't get just saved object")
    }
}

主要概念是 write/read 关注点的正确用法。对于这种情况 majority 需要关注。

"After the write operation returns with a w: "majority" acknowledgment to the client, the client can read the result of that write with a "majority" readConcern."

https://docs.mongodb.com/manual/reference/write-concern/ https://docs.mongodb.com/manual/reference/read-concern/