Java Code Examples for com.mongodb.client.model.UpdateOptions#upsert()
The following examples show how to use
com.mongodb.client.model.UpdateOptions#upsert() .
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: MongoDBClient.java From redtorch with MIT License | 5 votes |
/** * 更新或插入数据集合 * * @param dbName * @param collectionName * @param document * @param filter * @return */ public boolean upsertMany(String dbName, String collectionName, Document document, Document filter) { if (document != null) { UpdateOptions updateOptions = new UpdateOptions(); updateOptions.upsert(true); mongoClient.getDatabase(dbName).getCollection(collectionName).updateMany(filter, document, updateOptions); return true; } return false; }
Example 2
Source File: MongoBulkWriter.java From MongoSyphon with Apache License 2.0 | 5 votes |
public void Save(Document doc) { if (!doc.containsKey("_id")) { Create(doc); return; } Document find = new Document("_id", doc.get("_id")); UpdateOptions uo = new UpdateOptions(); uo.upsert(true); ops.add(new ReplaceOneModel<Document>(find, doc, uo)); FlushOpsIfFull(); }
Example 3
Source File: MongoMetadataDaoImpl.java From eagle with Apache License 2.0 | 5 votes |
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; }
Example 4
Source File: Repositories.java From immutables with Apache License 2.0 | 2 votes |
/** * Perform upsert: update single element or inserts a new one if none of the document matches. * <p> * <em>Note: Upsert operation requires special care to set or init all required attributes in case of insertion * (including but not limited to '_id'), so that valid document could be inserted into collection. * </em> * @return future of number of processed document (expected to be 1) */ public FluentFuture<Integer> upsert() { UpdateOptions options = new UpdateOptions(); options.upsert(true); return repository.doUpdate(criteria, collectRequiredUpdate(), options); }