mongodb 3.3.0 上面的 wasAcknowledged 标志实现
wasAcknowledged flag implementation in mongodb 3.3.0 above
我们现有的代码包含一些基于集合操作的 writeResult 的操作,例如:
WriteResult writeResult = dbCollection.insert(new BasicDBObject(someDoc));
if(writeResult.wasAcknowledged()) {
response.setSuccess(true);
}
使用升级后的 mongo 更改操作会导致如下结果:
WriteResult writeResult = dbCollection.insertOne(new BasicDBObject(someDoc)); // this won't work
if (writeResult.wasAcknowledged()) {
response.setSuccess(true);
}
问题是对集合的操作是 void
并且如果我查看文档 -
WriteResult.java
/**
* Returns true if the write was acknowledged.
*
* @return true if the write was acknowledged
* @see com.mongodb.WriteConcern#UNACKNOWLEDGED
* @since 3.0
*/
public boolean wasAcknowledged() {
return acknowledged;
}
WriteConcern.java
/**
* Write operations that use this write concern will return as soon as the message is written to the socket. Exceptions are raised for
* network issues, but not server errors.
*
* @since 2.10.0
* @mongodb.driver.manual core/write-concern/#unacknowledged Unacknowledged
*/
public static final WriteConcern UNACKNOWLEDGED = new WriteConcern(0);
我尝试修改代码以获得如下结果:
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).insertOne(document);
但是,我现在如何使用当前代码实现先前逻辑中的条件部分?
根据 MongoCollection
的文档
/**
* Inserts the provided document. If the document is missing an identifier, the driver should generate one.
*
* @param document the document to insert
* @throws com.mongodb.MongoWriteException if the write failed due some other failure specific to the insert command
* @throws com.mongodb.MongoWriteConcernException if the write failed due being unable to fulfil the write concern
* @throws com.mongodb.MongoException if the write failed due some other failure
*/
void insertOne(TDocument document);
如果由于无法满足写入问题而导致写入失败,插入操作应抛出 com.mongodb.MongoWriteConcernException
因此您的代码可以转换为:
try {
dbCollection
.withWriteConcern(WriteConcern.ACKNOWLEDGED)
.insertOne(new BasicDBObject(someDoc));
response.setSuccess(true);
} catch (MongoWriteConcernException x) {
response.setSuccess(false);
}
我们现有的代码包含一些基于集合操作的 writeResult 的操作,例如:
WriteResult writeResult = dbCollection.insert(new BasicDBObject(someDoc));
if(writeResult.wasAcknowledged()) {
response.setSuccess(true);
}
使用升级后的 mongo 更改操作会导致如下结果:
WriteResult writeResult = dbCollection.insertOne(new BasicDBObject(someDoc)); // this won't work
if (writeResult.wasAcknowledged()) {
response.setSuccess(true);
}
问题是对集合的操作是 void
并且如果我查看文档 -
WriteResult.java
/**
* Returns true if the write was acknowledged.
*
* @return true if the write was acknowledged
* @see com.mongodb.WriteConcern#UNACKNOWLEDGED
* @since 3.0
*/
public boolean wasAcknowledged() {
return acknowledged;
}
WriteConcern.java
/**
* Write operations that use this write concern will return as soon as the message is written to the socket. Exceptions are raised for
* network issues, but not server errors.
*
* @since 2.10.0
* @mongodb.driver.manual core/write-concern/#unacknowledged Unacknowledged
*/
public static final WriteConcern UNACKNOWLEDGED = new WriteConcern(0);
我尝试修改代码以获得如下结果:
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).insertOne(document);
但是,我现在如何使用当前代码实现先前逻辑中的条件部分?
根据 MongoCollection
/**
* Inserts the provided document. If the document is missing an identifier, the driver should generate one.
*
* @param document the document to insert
* @throws com.mongodb.MongoWriteException if the write failed due some other failure specific to the insert command
* @throws com.mongodb.MongoWriteConcernException if the write failed due being unable to fulfil the write concern
* @throws com.mongodb.MongoException if the write failed due some other failure
*/
void insertOne(TDocument document);
如果由于无法满足写入问题而导致写入失败,插入操作应抛出 com.mongodb.MongoWriteConcernException
因此您的代码可以转换为:
try {
dbCollection
.withWriteConcern(WriteConcern.ACKNOWLEDGED)
.insertOne(new BasicDBObject(someDoc));
response.setSuccess(true);
} catch (MongoWriteConcernException x) {
response.setSuccess(false);
}