Java Code Examples for com.mongodb.client.result.UpdateResult#getUpsertedId()

The following examples show how to use com.mongodb.client.result.UpdateResult#getUpsertedId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdateOneOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
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()
  );
}
 
Example 2
Source File: UpdateManyOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
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()
  );
}
 
Example 3
Source File: MongoMetadataDaoImpl.java    From eagle with Apache License 2.0 5 votes vote down vote up
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;
}