MongoDB Java客户端的WriteConcern不起作用 [英] MongoDB java client's WriteConcern doesn't work
问题描述
我是使用MongoDB的新手.我刚刚通过Maven导入了最新的MongoDB Java客户端:
I am a newbie to use MongoDB. I just imported the latest MongoDB java client via Maven:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
然后,我编写了一个非常简单的程序来测试插入操作.
And then I wrote a very simple program to test the insert operation.
//I use a replicaset
MongoClient mongoClient = new MongoClient(
Arrays.asList(new ServerAddress("10.12.16.136", 29017),
new ServerAddress("10.12.16.136", 29018),
new ServerAddress("10.12.16.136", 29019)));
//I just want to write and ignore any database errors. (This does not work)
mongoClient.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
//Get database and collection
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("realtime");
//This does not work too.
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED);
//Make a simple object to insert. I already have a document with the same _id in the database and I don't want to see the exception via the WriteConcern operation.
Document doc = Document("longitude", longitude)
.append("latitude", latitude)
.append("velocity", velocity)
.append("soc", soc)
.append("_id", 12345678);
//WriteConcern.UNACKNOWLEDGED doesn't work. An exception will be raised.
collection.insertOne(doc);
我该怎么做正确的事?
推荐答案
这是因为collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED);
生成了一个新的MongoCollection
对象,该对象具有不同的写顾虑,而您从未使用过:
That's because collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED);
generates a new MongoCollection
object with a different write concern which you never use:
/** *创建一个具有不同写入关注点的新MongoCollection实例. * * @param writeConcern集合的新{@link com.mongodb.WriteConcern} * @返回具有不同writeConcern的新MongoCollection实例 */
/** * Create a new MongoCollection instance with a different write concern. * * @param writeConcern the new {@link com.mongodb.WriteConcern} for the collection * @return a new MongoCollection instance with the different writeConcern */
MongoCollection withWriteConcern(WriteConcern writeConcern);
以下代码:
MongoCollection<Document> dup = collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED);
...
dup.insertOne(doc);
应该可以工作,即没有出现错误.
should work, i.e. no error raised.
关于MongoClient
级别的写关注,它不会传播到数据库:
As for the MongoClient
level write concern which is not propagated to the database:
public MongoDatabase getDatabase(final String databaseName) {
MongoClientOptions clientOptions = getMongoClientOptions();
return new MongoDatabaseImpl(databaseName, clientOptions.getCodecRegistry(), clientOptions.getReadPreference(),
clientOptions.getWriteConcern(), createOperationExecutor());
}
如您所见,写关注点来自MongoClientOptions
,而忽略了传递给mongoClient.setWriteConcern()
方法的参数值,这可能是一个错误.
As you can see, the write concern is taken from MongoClientOptions
ignoring the parameter value passed to mongoClient.setWriteConcern()
method, which may be a bug.
因此,要正确设置全局写关注点,您将必须创建MongoClientOptions
的实例:
So, to set a global write concern properly, you will have to create an instance of MongoClientOptions
:
MongoClientOptions options = MongoClientOptions
.builder()
.writeConcern(WriteConcern.UNACKNOWLEDGED)
.build();
并将其传递给MongoClient
构造函数.
and pass it to the MongoClient
constructor.
这篇关于MongoDB Java客户端的WriteConcern不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!