Search in sources :

Example 26 with WriteConcern

use of com.mongodb.WriteConcern in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getBulkWriteResult.

BsonDocument getBulkWriteResult(final BsonDocument arguments) {
    WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
    if (arguments.containsKey("writeConcern")) {
        if (arguments.getDocument("writeConcern").size() > 1) {
            throw new UnsupportedOperationException("Write concern document contains unexpected keys: " + arguments.getDocument("writeConcern").keySet());
        }
        writeConcern = new WriteConcern(arguments.getDocument("writeConcern").getInt32("w").intValue());
    }
    List<WriteModel<BsonDocument>> writeModels = new ArrayList<WriteModel<BsonDocument>>();
    for (Iterator<BsonValue> iter = arguments.getArray("requests").iterator(); iter.hasNext(); ) {
        BsonDocument cur = iter.next().asDocument();
        if (cur.get("insertOne") != null) {
            writeModels.add(new InsertOneModel<BsonDocument>(cur.getDocument("insertOne").getDocument("document")));
        } else if (cur.get("updateOne") != null) {
            writeModels.add(new UpdateOneModel<BsonDocument>(cur.getDocument("updateOne").getDocument("filter"), cur.getDocument("updateOne").getDocument("update")));
        } else {
            throw new UnsupportedOperationException("Unsupported write request type");
        }
    }
    return toResult(collection.withWriteConcern(writeConcern).bulkWrite(writeModels, new BulkWriteOptions().ordered(arguments.getBoolean("ordered", BsonBoolean.TRUE).getValue())));
}
Also used : UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BsonDocument(org.bson.BsonDocument) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) WriteConcern(com.mongodb.WriteConcern) ArrayList(java.util.ArrayList) WriteModel(com.mongodb.client.model.WriteModel) BsonValue(org.bson.BsonValue)

Example 27 with WriteConcern

use of com.mongodb.WriteConcern in project spring-cloud-connectors by spring-cloud.

the class MongoDbFactoryCreator method getMongoOptions.

private MongoClientOptions.Builder getMongoOptions(MongoDbFactoryConfig config) {
    MongoClientOptions.Builder builder;
    Method builderMethod = ClassUtils.getMethodIfAvailable(MongoClientOptions.class, "builder");
    if (builderMethod != null) {
        builder = (Builder) ReflectionUtils.invokeMethod(builderMethod, null);
    } else {
        Constructor<Builder> builderConstructor = ClassUtils.getConstructorIfAvailable(MongoClientOptions.Builder.class);
        try {
            builder = builderConstructor.newInstance(new Object[0]);
        } catch (InstantiationException e) {
            throw new IllegalStateException(e);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e);
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException(e);
        } catch (InvocationTargetException e) {
            throw new IllegalStateException(e);
        }
    }
    if (config != null) {
        if (config.getConnectionsPerHost() != null) {
            builder.connectionsPerHost(config.getConnectionsPerHost());
        }
        if (config.getMaxWaitTime() != null) {
            builder.maxWaitTime(config.getMaxWaitTime());
        }
        if (config.getWriteConcern() != null) {
            builder.writeConcern(new WriteConcern(config.getWriteConcern()));
        }
    }
    return builder;
}
Also used : MongoClientOptions(com.mongodb.MongoClientOptions) WriteConcern(com.mongodb.WriteConcern) Builder(com.mongodb.MongoClientOptions.Builder) Method(java.lang.reflect.Method) Builder(com.mongodb.MongoClientOptions.Builder) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 28 with WriteConcern

use of com.mongodb.WriteConcern in project camel by apache.

the class MongoDbEndpoint method setWriteConcernRef.

/**
     * Set the {@link WriteConcern} for write operations on MongoDB, passing in
     * the bean ref to a custom WriteConcern which exists in the Registry. You
     * can also use standard WriteConcerns by passing in their key. See the
     * {@link #setWriteConcern(String) setWriteConcern} method.
     * 
     * @param writeConcernRef the name of the bean in the registry that
     *            represents the WriteConcern to use
     */
public void setWriteConcernRef(String writeConcernRef) {
    WriteConcern wc = this.getCamelContext().getRegistry().lookupByNameAndType(writeConcernRef, WriteConcern.class);
    if (wc == null) {
        String msg = "Camel MongoDB component could not find the WriteConcern in the Registry. Verify that the " + "provided bean name (" + writeConcernRef + ")  is correct. Aborting initialization.";
        throw new IllegalArgumentException(msg);
    }
    this.writeConcernRef = wc;
}
Also used : WriteConcern(com.mongodb.WriteConcern)

Example 29 with WriteConcern

use of com.mongodb.WriteConcern in project camel by apache.

the class GridFsEndpoint method setWriteConcernRef.

/**
     * Set the {@link WriteConcern} for write operations on MongoDB, passing in the bean ref to a custom WriteConcern which exists in the Registry.
     * You can also use standard WriteConcerns by passing in their key. See the {@link #setWriteConcern(String) setWriteConcern} method.
     * 
     * @param writeConcernRef the name of the bean in the registry that represents the WriteConcern to use
     */
public void setWriteConcernRef(String writeConcernRef) {
    WriteConcern wc = this.getCamelContext().getRegistry().lookupByNameAndType(writeConcernRef, WriteConcern.class);
    if (wc == null) {
        String msg = "Camel MongoDB component could not find the WriteConcern in the Registry. Verify that the " + "provided bean name (" + writeConcernRef + ")  is correct. Aborting initialization.";
        throw new IllegalArgumentException(msg);
    }
    this.writeConcernRef = wc;
}
Also used : WriteConcern(com.mongodb.WriteConcern)

Example 30 with WriteConcern

use of com.mongodb.WriteConcern in project camel by apache.

the class MongoDbEndpoint method setWriteConcernRef.

/**
     * Set the {@link WriteConcern} for write operations on MongoDB, passing in the bean ref to a custom WriteConcern which exists in the Registry.
     * You can also use standard WriteConcerns by passing in their key. See the {@link #setWriteConcern(String) setWriteConcern} method.
     * 
     * @param writeConcernRef the name of the bean in the registry that represents the WriteConcern to use
     */
public void setWriteConcernRef(String writeConcernRef) {
    WriteConcern wc = this.getCamelContext().getRegistry().lookupByNameAndType(writeConcernRef, WriteConcern.class);
    if (wc == null) {
        String msg = "Camel MongoDB component could not find the WriteConcern in the Registry. Verify that the " + "provided bean name (" + writeConcernRef + ")  is correct. Aborting initialization.";
        throw new IllegalArgumentException(msg);
    }
    this.writeConcernRef = wc;
}
Also used : WriteConcern(com.mongodb.WriteConcern)

Aggregations

WriteConcern (com.mongodb.WriteConcern)53 Document (org.bson.Document)14 MongoException (com.mongodb.MongoException)11 List (java.util.List)11 ArrayList (java.util.ArrayList)9 ReadPreference (com.mongodb.ReadPreference)8 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 TimeUnit (java.util.concurrent.TimeUnit)7 BsonDocument (org.bson.BsonDocument)7 DeleteResult (com.mongodb.client.result.DeleteResult)6 HashMap (java.util.HashMap)6 ClientSessionOptions (com.mongodb.ClientSessionOptions)5 ReadConcern (com.mongodb.ReadConcern)5 Arrays (java.util.Arrays)5 BsonValue (org.bson.BsonValue)5 FullDocument (com.mongodb.client.model.changestream.FullDocument)4 InsertOneResult (com.mongodb.client.result.InsertOneResult)4 MongoClient (com.mongodb.reactivestreams.client.MongoClient)4 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)4