下面列出了com.mongodb.client.result.UpdateResult#getUpsertedId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public SyncUpdateResult execute(@Nullable final CoreStitchServiceClient service) {
final UpdateResult localResult = this.dataSynchronizer.updateOne(
namespace,
filter,
update,
new UpdateOptions().upsert(this.syncUpdateOptions.isUpsert()));
return new SyncUpdateResult(
localResult.getMatchedCount(),
localResult.getModifiedCount(),
localResult.getUpsertedId()
);
}
public SyncUpdateResult execute(@Nullable final CoreStitchServiceClient service) {
final UpdateResult localResult = this.dataSynchronizer.updateMany(
namespace,
filter,
update,
new UpdateOptions().upsert(this.syncUpdateOptions.isUpsert()));
return new SyncUpdateResult(
localResult.getMatchedCount(),
localResult.getModifiedCount(),
localResult.getUpsertedId()
);
}
private <T> OpResult addOrReplace(MongoCollection<Document> collection, T t) {
BsonDocument filter = new BsonDocument();
if (t instanceof StreamDefinition) {
filter.append("streamId", new BsonString(MetadataUtils.getKey(t)));
} else if (t instanceof AlertPublishEvent) {
filter.append("alertId", new BsonString(MetadataUtils.getKey(t)));
} else {
filter.append("name", new BsonString(MetadataUtils.getKey(t)));
}
String json = "";
OpResult result = new OpResult();
try {
json = mapper.writeValueAsString(t);
UpdateOptions options = new UpdateOptions();
options.upsert(true);
UpdateResult ur = collection.replaceOne(filter, Document.parse(json), options);
// FIXME: could based on matched count do better matching...
if (ur.getModifiedCount() > 0 || ur.getUpsertedId() != null) {
result.code = 200;
result.message = String.format("update %d configuration item.", ur.getModifiedCount());
} else {
result.code = 500;
result.message = "no configuration item create/updated.";
}
} catch (Exception e) {
result.code = 500;
result.message = e.getMessage();
LOG.error("", e);
}
return result;
}