use of com.mongodb.client.model.UpdateOneModel in project mongo-java-driver by mongodb.
the class MongoCollectionImpl method bulkWrite.
@SuppressWarnings("unchecked")
@Override
public void bulkWrite(final List<? extends WriteModel<? extends TDocument>> requests, final BulkWriteOptions options, final SingleResultCallback<BulkWriteResult> callback) {
notNull("requests", requests);
List<WriteRequest> writeRequests = new ArrayList<WriteRequest>(requests.size());
for (WriteModel<? extends TDocument> writeModel : requests) {
WriteRequest writeRequest;
if (writeModel == null) {
throw new IllegalArgumentException("requests can not contain a null value");
} else if (writeModel instanceof InsertOneModel) {
TDocument document = ((InsertOneModel<TDocument>) writeModel).getDocument();
if (getCodec() instanceof CollectibleCodec) {
document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
}
writeRequest = new InsertRequest(documentToBsonDocument(document));
} else if (writeModel instanceof ReplaceOneModel) {
ReplaceOneModel<TDocument> replaceOneModel = (ReplaceOneModel<TDocument>) writeModel;
writeRequest = new UpdateRequest(toBsonDocument(replaceOneModel.getFilter()), documentToBsonDocument(replaceOneModel.getReplacement()), WriteRequest.Type.REPLACE).upsert(replaceOneModel.getOptions().isUpsert()).collation(replaceOneModel.getOptions().getCollation());
} else if (writeModel instanceof UpdateOneModel) {
UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel;
writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), toBsonDocument(updateOneModel.getUpdate()), WriteRequest.Type.UPDATE).multi(false).upsert(updateOneModel.getOptions().isUpsert()).collation(updateOneModel.getOptions().getCollation());
} else if (writeModel instanceof UpdateManyModel) {
UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
writeRequest = new UpdateRequest(toBsonDocument(updateManyModel.getFilter()), toBsonDocument(updateManyModel.getUpdate()), WriteRequest.Type.UPDATE).multi(true).upsert(updateManyModel.getOptions().isUpsert()).collation(updateManyModel.getOptions().getCollation());
} else if (writeModel instanceof DeleteOneModel) {
DeleteOneModel<TDocument> deleteOneModel = (DeleteOneModel<TDocument>) writeModel;
writeRequest = new DeleteRequest(toBsonDocument(deleteOneModel.getFilter())).multi(false).collation(deleteOneModel.getOptions().getCollation());
} else if (writeModel instanceof DeleteManyModel) {
DeleteManyModel<TDocument> deleteManyModel = (DeleteManyModel<TDocument>) writeModel;
writeRequest = new DeleteRequest(toBsonDocument(deleteManyModel.getFilter())).multi(true).collation(deleteManyModel.getOptions().getCollation());
} else {
throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));
}
writeRequests.add(writeRequest);
}
executor.execute(new MixedBulkWriteOperation(namespace, writeRequests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()), callback);
}
use of com.mongodb.client.model.UpdateOneModel in project mongo-java-driver by mongodb.
the class MongoCollectionImpl method bulkWrite.
@Override
@SuppressWarnings("unchecked")
public BulkWriteResult bulkWrite(final List<? extends WriteModel<? extends TDocument>> requests, final BulkWriteOptions options) {
notNull("requests", requests);
List<WriteRequest> writeRequests = new ArrayList<WriteRequest>(requests.size());
for (WriteModel<? extends TDocument> writeModel : requests) {
WriteRequest writeRequest;
if (writeModel == null) {
throw new IllegalArgumentException("requests can not contain a null value");
} else if (writeModel instanceof InsertOneModel) {
TDocument document = ((InsertOneModel<TDocument>) writeModel).getDocument();
if (getCodec() instanceof CollectibleCodec) {
document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
}
writeRequest = new InsertRequest(documentToBsonDocument(document));
} else if (writeModel instanceof ReplaceOneModel) {
ReplaceOneModel<TDocument> replaceOneModel = (ReplaceOneModel<TDocument>) writeModel;
writeRequest = new UpdateRequest(toBsonDocument(replaceOneModel.getFilter()), documentToBsonDocument(replaceOneModel.getReplacement()), WriteRequest.Type.REPLACE).upsert(replaceOneModel.getOptions().isUpsert()).collation(replaceOneModel.getOptions().getCollation());
} else if (writeModel instanceof UpdateOneModel) {
UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel;
writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), toBsonDocument(updateOneModel.getUpdate()), WriteRequest.Type.UPDATE).multi(false).upsert(updateOneModel.getOptions().isUpsert()).collation(updateOneModel.getOptions().getCollation());
} else if (writeModel instanceof UpdateManyModel) {
UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
writeRequest = new UpdateRequest(toBsonDocument(updateManyModel.getFilter()), toBsonDocument(updateManyModel.getUpdate()), WriteRequest.Type.UPDATE).multi(true).upsert(updateManyModel.getOptions().isUpsert()).collation(updateManyModel.getOptions().getCollation());
} else if (writeModel instanceof DeleteOneModel) {
DeleteOneModel<TDocument> deleteOneModel = (DeleteOneModel<TDocument>) writeModel;
writeRequest = new DeleteRequest(toBsonDocument(deleteOneModel.getFilter())).multi(false).collation(deleteOneModel.getOptions().getCollation());
} else if (writeModel instanceof DeleteManyModel) {
DeleteManyModel<TDocument> deleteManyModel = (DeleteManyModel<TDocument>) writeModel;
writeRequest = new DeleteRequest(toBsonDocument(deleteManyModel.getFilter())).multi(true).collation(deleteManyModel.getOptions().getCollation());
} else {
throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));
}
writeRequests.add(writeRequest);
}
return executor.execute(new MixedBulkWriteOperation(namespace, writeRequests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()));
}
use of com.mongodb.client.model.UpdateOneModel in project YCSB by brianfrankcooper.
the class MongoDbClient method insert.
/**
* Insert a record in the database. Any field/value pairs in the specified
* values HashMap will be written into the record with the specified record
* key.
*
* @param table
* The name of the table
* @param key
* The record key of the record to insert.
* @param values
* A HashMap of field/value pairs to insert in the record
* @return Zero on success, a non-zero error code on error. See the {@link DB}
* class's description for a discussion of error codes.
*/
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
try {
MongoCollection<Document> collection = database.getCollection(table);
Document toInsert = new Document("_id", key);
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
toInsert.put(entry.getKey(), entry.getValue().toArray());
}
if (batchSize == 1) {
if (useUpsert) {
// this is effectively an insert, but using an upsert instead due
// to current inability of the framework to clean up after itself
// between test runs.
collection.replaceOne(new Document("_id", toInsert.get("_id")), toInsert, UPDATE_WITH_UPSERT);
} else {
collection.insertOne(toInsert);
}
} else {
bulkInserts.add(toInsert);
if (bulkInserts.size() == batchSize) {
if (useUpsert) {
List<UpdateOneModel<Document>> updates = new ArrayList<UpdateOneModel<Document>>(bulkInserts.size());
for (Document doc : bulkInserts) {
updates.add(new UpdateOneModel<Document>(new Document("_id", doc.get("_id")), doc, UPDATE_WITH_UPSERT));
}
collection.bulkWrite(updates);
} else {
collection.insertMany(bulkInserts, INSERT_UNORDERED);
}
bulkInserts.clear();
} else {
return Status.BATCHED_OK;
}
}
return Status.OK;
} catch (Exception e) {
System.err.println("Exception while trying bulk insert with " + bulkInserts.size());
e.printStackTrace();
return Status.ERROR;
}
}
use of com.mongodb.client.model.UpdateOneModel 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())));
}
Aggregations