List of usage examples for com.mongodb.client.result UpdateResult getUpsertedId
@Nullable public abstract BsonValue getUpsertedId();
From source file:org.apache.eagle.alert.metadata.impl.MongoMetadataDaoImpl.java
License:Apache License
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 PublishmentType) { filter.append("type", new BsonString(MetadataUtils.getKey(t))); } else if (t instanceof AlertPublishEvent) { filter.append("alertId", new BsonString(MetadataUtils.getKey(t))); } else {//from www .ja va2s . c o m 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; }
From source file:org.siphon.jsmongo.MongoExecutor.java
License:Open Source License
public Object updateRow(String table, ScriptObjectMirror row1, ScriptObjectMirror row2) throws SqlExecutorException, ScriptException { MongoCollection<Document> collection = database.getCollection(table); Document document = jsObjectToDocument(row1); Document document2 = jsObjectToDocument(row2); UpdateResult result = collection.updateOne(document, document2); return bsonObjectToJsObject(result.getUpsertedId()); }