Search in sources :

Example 31 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project nifi by apache.

the class PutMongo method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).getValue());
    final String mode = context.getProperty(MODE).getValue();
    final String updateMode = context.getProperty(UPDATE_MODE).getValue();
    final WriteConcern writeConcern = getWriteConcern(context);
    final MongoCollection<Document> collection = getCollection(context, flowFile).withWriteConcern(writeConcern);
    try {
        // Read the contents of the FlowFile into a byte array
        final byte[] content = new byte[(int) flowFile.getSize()];
        session.read(flowFile, in -> StreamUtils.fillBuffer(in, content, true));
        // parse
        final Object doc = (mode.equals(MODE_INSERT) || (mode.equals(MODE_UPDATE) && updateMode.equals(UPDATE_WITH_DOC.getValue()))) ? Document.parse(new String(content, charset)) : JSON.parse(new String(content, charset));
        if (MODE_INSERT.equalsIgnoreCase(mode)) {
            collection.insertOne((Document) doc);
            logger.info("inserted {} into MongoDB", new Object[] { flowFile });
        } else {
            // update
            final boolean upsert = context.getProperty(UPSERT).asBoolean();
            final String updateKey = context.getProperty(UPDATE_QUERY_KEY).evaluateAttributeExpressions(flowFile).getValue();
            final String filterQuery = context.getProperty(UPDATE_QUERY).evaluateAttributeExpressions(flowFile).getValue();
            final Document query;
            if (!StringUtils.isBlank(updateKey)) {
                query = parseUpdateKey(updateKey, (Map) doc);
                removeUpdateKeys(updateKey, (Map) doc);
            } else {
                query = Document.parse(filterQuery);
            }
            if (updateMode.equals(UPDATE_WITH_DOC.getValue())) {
                collection.replaceOne(query, (Document) doc, new UpdateOptions().upsert(upsert));
            } else {
                BasicDBObject update = (BasicDBObject) doc;
                update.remove(updateKey);
                collection.updateOne(query, update, new UpdateOptions().upsert(upsert));
            }
            logger.info("updated {} into MongoDB", new Object[] { flowFile });
        }
        session.getProvenanceReporter().send(flowFile, getURI(context));
        session.transfer(flowFile, REL_SUCCESS);
    } catch (Exception e) {
        logger.error("Failed to insert {} into MongoDB due to {}", new Object[] { flowFile, e }, e);
        session.transfer(flowFile, REL_FAILURE);
        context.yield();
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Charset(java.nio.charset.Charset) Document(org.bson.Document) ComponentLog(org.apache.nifi.logging.ComponentLog) UpdateOptions(com.mongodb.client.model.UpdateOptions) ProcessException(org.apache.nifi.processor.exception.ProcessException) BasicDBObject(com.mongodb.BasicDBObject) WriteConcern(com.mongodb.WriteConcern) BasicDBObject(com.mongodb.BasicDBObject) Map(java.util.Map)

Example 32 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method doUpdate.

protected Mono<UpdateResult> doUpdate(final String collectionName, @Nullable Query query, @Nullable Update update, @Nullable Class<?> entityClass, final boolean upsert, final boolean multi) {
    MongoPersistentEntity<?> entity = entityClass == null ? null : getPersistentEntity(entityClass);
    Flux<UpdateResult> result = execute(collectionName, collection -> {
        increaseVersionForUpdateIfNecessary(entity, update);
        Document queryObj = query == null ? new Document() : queryMapper.getMappedObject(query.getQueryObject(), entity);
        Document updateObj = update == null ? new Document() : updateMapper.getMappedObject(update.getUpdateObject(), entity);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(String.format("Calling update using query: %s and update: %s in collection: %s", serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName));
        }
        MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName, entityClass, updateObj, queryObj);
        WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
        MongoCollection<Document> collectionToUse = prepareCollection(collection, writeConcernToUse);
        UpdateOptions updateOptions = new UpdateOptions().upsert(upsert);
        query.getCollation().map(Collation::toMongoCollation).ifPresent(updateOptions::collation);
        if (!UpdateMapper.isUpdateObject(updateObj)) {
            return collectionToUse.replaceOne(queryObj, updateObj, updateOptions);
        }
        if (multi) {
            return collectionToUse.updateMany(queryObj, updateObj, updateOptions);
        }
        return collectionToUse.updateOne(queryObj, updateObj, updateOptions);
    }).doOnNext(updateResult -> {
        if (entity != null && entity.hasVersionProperty() && !multi) {
            if (updateResult.wasAcknowledged() && updateResult.getMatchedCount() == 0) {
                Document queryObj = query == null ? new Document() : queryMapper.getMappedObject(query.getQueryObject(), entity);
                Document updateObj = update == null ? new Document() : updateMapper.getMappedObject(update.getUpdateObject(), entity);
                if (dbObjectContainsVersionProperty(queryObj, entity))
                    throw new OptimisticLockingFailureException("Optimistic lock exception on saving entity: " + updateObj.toString() + " to collection " + collectionName);
            }
        }
    });
    return result.next();
}
Also used : Document(org.bson.Document) PropertyReferenceException(org.springframework.data.mapping.PropertyReferenceException) DeleteOptions(com.mongodb.client.model.DeleteOptions) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ClientSession(com.mongodb.session.ClientSession) FullDocument(com.mongodb.client.model.changestream.FullDocument) Optionals(org.springframework.data.util.Optionals) UpdateResult(com.mongodb.client.result.UpdateResult) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) UpdateOptions(com.mongodb.client.model.UpdateOptions) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) ClassUtils(org.springframework.util.ClassUtils) NonNull(lombok.NonNull) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) PropertyPath(org.springframework.data.mapping.PropertyPath) Id(org.springframework.data.annotation.Id) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AfterDeleteEvent(org.springframework.data.mongodb.core.mapping.event.AfterDeleteEvent) java.util(java.util) Validator(org.springframework.data.mongodb.core.validation.Validator) MappingContext(org.springframework.data.mapping.context.MappingContext) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) CountOptions(com.mongodb.client.model.CountOptions) MongoClient(com.mongodb.reactivestreams.client.MongoClient) Bson(org.bson.conversions.Bson) Filters(com.mongodb.client.model.Filters) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) MappingException(org.springframework.data.mapping.MappingException) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) Nullable(org.springframework.lang.Nullable) MongoSimpleTypes(org.springframework.data.mongodb.core.mapping.MongoSimpleTypes) Success(com.mongodb.reactivestreams.client.Success) ConversionService(org.springframework.core.convert.ConversionService) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) AfterConvertEvent(org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent) BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) ReactiveIndexOperations(org.springframework.data.mongodb.core.index.ReactiveIndexOperations) Publisher(org.reactivestreams.Publisher) ObjectUtils(org.springframework.util.ObjectUtils) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) Mono(reactor.core.publisher.Mono) BeansException(org.springframework.beans.BeansException) MongoMappingEventPublisher(org.springframework.data.mongodb.core.index.MongoMappingEventPublisher) Criteria(org.springframework.data.mongodb.core.query.Criteria) Flux(reactor.core.publisher.Flux) ObjectId(org.bson.types.ObjectId) MongoMappingEvent(org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent) org.springframework.data.mongodb.core.convert(org.springframework.data.mongodb.core.convert) JSONParseException(com.mongodb.util.JSONParseException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LoggerFactory(org.slf4j.LoggerFactory) Collation(org.springframework.data.mongodb.core.query.Collation) MongoClientVersion(org.springframework.data.mongodb.util.MongoClientVersion) BeforeDeleteEvent(org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent) BsonValue(org.bson.BsonValue) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) com.mongodb(com.mongodb) Pair(org.springframework.data.util.Pair) Update(org.springframework.data.mongodb.core.query.Update) MongoPersistentEntityIndexCreator(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator) SerializationUtils(org.springframework.data.mongodb.core.query.SerializationUtils) ReturnDocument(com.mongodb.client.model.ReturnDocument) ApplicationListener(org.springframework.context.ApplicationListener) Collectors(java.util.stream.Collectors) Entry(java.util.Map.Entry) Codec(org.bson.codecs.Codec) ReactiveMongoDatabaseFactory(org.springframework.data.mongodb.ReactiveMongoDatabaseFactory) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) Metric(org.springframework.data.geo.Metric) DataAccessException(org.springframework.dao.DataAccessException) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Tuple2(reactor.util.function.Tuple2) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) DistinctPublisher(com.mongodb.reactivestreams.client.DistinctPublisher) Function(java.util.function.Function) Distance(org.springframework.data.geo.Distance) EntityReader(org.springframework.data.convert.EntityReader) AfterLoadEvent(org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) ChangeStreamPublisher(com.mongodb.reactivestreams.client.ChangeStreamPublisher) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) ValidationOptions(com.mongodb.client.model.ValidationOptions) Nonnull(javax.annotation.Nonnull) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Logger(org.slf4j.Logger) AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) IndexOperationsAdapter(org.springframework.data.mongodb.core.index.IndexOperationsAdapter) ApplicationContext(org.springframework.context.ApplicationContext) Query(org.springframework.data.mongodb.core.query.Query) Consumer(java.util.function.Consumer) AfterSaveEvent(org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent) AggregatePublisher(com.mongodb.reactivestreams.client.AggregatePublisher) DeleteResult(com.mongodb.client.result.DeleteResult) GeoResult(org.springframework.data.geo.GeoResult) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) Collation(org.springframework.data.mongodb.core.query.Collation) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions)

Example 33 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project LuckPerms by lucko.

the class MongoDao method savePlayerData.

@Override
public PlayerSaveResult savePlayerData(UUID uuid, String username) {
    username = username.toLowerCase();
    MongoCollection<Document> c = this.database.getCollection(this.prefix + "uuid");
    // find any existing mapping
    String oldUsername = getPlayerName(uuid);
    // do the insert
    if (!username.equalsIgnoreCase(oldUsername)) {
        c.replaceOne(new Document("_id", uuid), new Document("_id", uuid).append("name", username), new UpdateOptions().upsert(true));
    }
    PlayerSaveResult result = PlayerSaveResult.determineBaseResult(username, oldUsername);
    Set<UUID> conflicting = new HashSet<>();
    try (MongoCursor<Document> cursor = c.find(new Document("name", username)).iterator()) {
        if (cursor.hasNext()) {
            conflicting.add(cursor.next().get("_id", UUID.class));
        }
    }
    conflicting.remove(uuid);
    if (!conflicting.isEmpty()) {
        // remove the mappings for conflicting uuids
        c.deleteMany(Filters.and(conflicting.stream().map(u -> Filters.eq("_id", u)).collect(Collectors.toList())));
        result = result.withOtherUuidsPresent(conflicting);
    }
    return result;
}
Also used : PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) LogEntry(me.lucko.luckperms.api.LogEntry) Document(org.bson.Document) MongoClientURI(com.mongodb.MongoClientURI) AbstractDao(me.lucko.luckperms.common.storage.dao.AbstractDao) MongoCredential(com.mongodb.MongoCredential) MongoCollection(com.mongodb.client.MongoCollection) UserIdentifier(me.lucko.luckperms.common.references.UserIdentifier) MongoDatabase(com.mongodb.client.MongoDatabase) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) BulkUpdate(me.lucko.luckperms.common.bulkupdate.BulkUpdate) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) LegacyNodeFactory(me.lucko.luckperms.common.node.LegacyNodeFactory) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) NodeModel(me.lucko.luckperms.common.node.NodeModel) MongoCursor(com.mongodb.client.MongoCursor) MongoClientOptions(com.mongodb.MongoClientOptions) Map(java.util.Map) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) HeldPermission(me.lucko.luckperms.api.HeldPermission) UpdateOptions(com.mongodb.client.model.UpdateOptions) ServerAddress(com.mongodb.ServerAddress) ExtendedLogEntry(me.lucko.luckperms.common.actionlog.ExtendedLogEntry) GroupManager(me.lucko.luckperms.common.managers.group.GroupManager) StorageCredentials(me.lucko.luckperms.common.storage.StorageCredentials) NodeHeldPermission(me.lucko.luckperms.common.node.NodeHeldPermission) Set(java.util.Set) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet) Log(me.lucko.luckperms.common.actionlog.Log) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) UUID(java.util.UUID) ContextSet(me.lucko.luckperms.api.context.ContextSet) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) TrackManager(me.lucko.luckperms.common.managers.track.TrackManager) MongoClient(com.mongodb.MongoClient) Group(me.lucko.luckperms.common.model.Group) Optional(java.util.Optional) Node(me.lucko.luckperms.api.Node) User(me.lucko.luckperms.common.model.User) Document(org.bson.Document) UUID(java.util.UUID) UpdateOptions(com.mongodb.client.model.UpdateOptions) HashSet(java.util.HashSet)

Example 34 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project duangframework by tcrct.

the class MongoBaseDao method update.

private boolean update(String id, Document document) throws Exception {
    if (!ObjectId.isValid(id)) {
        throw new MongodbException("id is not ObjectId!");
    }
    Document query = new Document(IdEntity.ID_FIELD, new ObjectId(id));
    // 查询记录不存在时,不新增记录
    UpdateOptions options = new UpdateOptions();
    options.upsert(false);
    document.remove(IdEntity.ENTITY_ID_FIELD);
    BasicDBObject updateDbo = new BasicDBObject(Operator.SET, document);
    return collection.updateOne(query, updateDbo, options).isModifiedCountAvailable();
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) MongodbException(com.duangframework.core.exceptions.MongodbException)

Example 35 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project webprotege by protegeproject.

the class UserRecordRepository method save.

public void save(UserRecord userRecord) {
    Document document = converter.toDocument(userRecord);
    collection.replaceOne(byUserId(userRecord.getUserId()), document, new UpdateOptions().upsert(true));
}
Also used : Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Aggregations

UpdateOptions (com.mongodb.client.model.UpdateOptions)53 Document (org.bson.Document)31 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)21 Bson (org.bson.conversions.Bson)17 UpdateResult (com.mongodb.client.result.UpdateResult)10 Test (org.junit.jupiter.api.Test)10 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)8 ArrayList (java.util.ArrayList)6 BsonDocument (org.bson.BsonDocument)6 Test (org.junit.Test)6 Update (org.springframework.data.mongodb.core.query.Update)4 BasicDBObject (com.mongodb.BasicDBObject)3 MongoCollection (com.mongodb.client.MongoCollection)3 CountOptions (com.mongodb.client.model.CountOptions)3 IOException (java.io.IOException)3 List (java.util.List)3 BsonValue (org.bson.BsonValue)3 ObjectId (org.bson.types.ObjectId)3 MongodbException (com.duangframework.core.exceptions.MongodbException)2 Block (com.mongodb.Block)2