use of com.mongodb.client.model.UpdateOptions in project core-ng-project by neowu.
the class MongoCollectionImpl method bulkReplace.
@Override
public void bulkReplace(List<T> entities) {
StopWatch watch = new StopWatch();
if (entities == null || entities.isEmpty())
throw Exceptions.error("entities must not be empty");
for (T entity : entities) {
validator.validate(entity);
}
try {
List<ReplaceOneModel<T>> models = new ArrayList<>(entities.size());
for (T entity : entities) {
Object id = mongo.codecs.id(entity);
if (id == null)
throw Exceptions.error("entity must have id, entityClass={}", entityClass.getCanonicalName());
models.add(new ReplaceOneModel<>(Filters.eq("_id", id), entity, new UpdateOptions().upsert(true)));
}
collection().bulkWrite(models, new BulkWriteOptions().ordered(false));
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("mongoDB", elapsedTime, 0, entities.size());
logger.debug("bulkReplace, collection={}, size={}, elapsedTime={}", collectionName, entities.size(), elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of com.mongodb.client.model.UpdateOptions in project core-ng-project by neowu.
the class MongoCollectionImpl method replace.
@Override
public void replace(T entity) {
StopWatch watch = new StopWatch();
Object id = null;
validator.validate(entity);
try {
id = mongo.codecs.id(entity);
if (id == null)
throw Exceptions.error("entity must have id, entityClass={}", entityClass.getCanonicalName());
collection().replaceOne(Filters.eq("_id", id), entity, new UpdateOptions().upsert(true));
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("mongoDB", elapsedTime, 0, 1);
logger.debug("replace, collection={}, id={}, elapsedTime={}", collectionName, id, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of com.mongodb.client.model.UpdateOptions in project engine by Lumeer.
the class MongoDbStorage method replaceDocument.
@Override
public void replaceDocument(final String collectionName, final DataDocument replaceDocument, final DataFilter filter) {
DataDocument toReplace = new DataDocument(replaceDocument);
if (toReplace.containsKey(DOCUMENT_ID)) {
toReplace.remove(DOCUMENT_ID);
}
Document replaceDoc = new Document(toReplace);
database.getCollection(collectionName).replaceOne(filter.<Bson>get(), replaceDoc, new UpdateOptions().upsert(true));
}
use of com.mongodb.client.model.UpdateOptions in project engine by Lumeer.
the class MongoUserDao method deleteGroupFromUsers.
@Override
public void deleteGroupFromUsers(final String organizationId, final String group) {
String key = MongoUtils.concatParams(UserCodec.ALL_GROUPS, "$[" + ELEMENT_NAME + "]", UserCodec.GROUPS);
Bson pullGroups = Updates.pull(key, group);
UpdateOptions options = new UpdateOptions().arrayFilters(arrayFilters(organizationId));
databaseCollection().updateMany(new BsonDocument(), pullGroups, options);
}
use of com.mongodb.client.model.UpdateOptions in project gora by apache.
the class MongoStore method performPut.
/**
* Update a object that already exists in the store. The object must exist
* already or the update may fail.
*
* @param key
* identifier of the object in the store
* @param obj
* the object to be inserted
*/
private void performPut(final K key, final T obj) {
// Build the query to select the object to be updated
Document qSel = new Document("_id", key);
// Build the update query
Document qUpdate = new Document();
Document qUpdateSet = newUpdateSetInstance(obj);
if (qUpdateSet.size() > 0) {
qUpdate.put("$set", qUpdateSet);
}
Document qUpdateUnset = newUpdateUnsetInstance(obj);
if (qUpdateUnset.size() > 0) {
qUpdate.put("$unset", qUpdateUnset);
}
// Execute the update (if there is at least one $set ot $unset
if (!qUpdate.isEmpty()) {
mongoClientColl.updateOne(qSel, qUpdate, new UpdateOptions().upsert(true));
obj.clearDirty();
} else {
LOG.debug("No update to perform, skip {}", key);
}
}
Aggregations