use of com.mongodb.client.model.DeleteOneModel 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.DeleteOneModel 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.DeleteOneModel in project core-ng-project by neowu.
the class MongoCollectionImpl method bulkDelete.
@Override
public long bulkDelete(List<?> ids) {
StopWatch watch = new StopWatch();
int deletedRows = 0;
try {
List<DeleteOneModel<T>> models = new ArrayList<>(ids.size());
for (Object id : ids) {
models.add(new DeleteOneModel<>(Filters.eq("_id", id)));
}
BulkWriteResult result = collection().bulkWrite(models, new BulkWriteOptions().ordered(false));
deletedRows = result.getDeletedCount();
return deletedRows;
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("mongoDB", elapsedTime, 0, deletedRows);
logger.debug("bulkDelete, collection={}, size={}, elapsedTime={}", collectionName, ids.size(), elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of com.mongodb.client.model.DeleteOneModel in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getBulkWriteResult.
BsonDocument getBulkWriteResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
List<WriteModel<BsonDocument>> writeModels = new ArrayList<WriteModel<BsonDocument>>();
for (BsonValue bsonValue : arguments.getArray("requests")) {
BsonDocument cur = bsonValue.asDocument();
String name = cur.getString("name").getValue();
BsonDocument requestArguments = cur.getDocument("arguments");
if (name.equals("insertOne")) {
writeModels.add(new InsertOneModel<BsonDocument>(requestArguments.getDocument("document")));
} else if (name.equals("updateOne")) {
if (requestArguments.isDocument("update")) {
writeModels.add(new UpdateOneModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("update"), getUpdateOptions(requestArguments)));
} else {
// update is a pipeline
writeModels.add(new UpdateOneModel<BsonDocument>(requestArguments.getDocument("filter"), getListOfDocuments(requestArguments.getArray("update")), getUpdateOptions(requestArguments)));
}
} else if (name.equals("updateMany")) {
if (requestArguments.isDocument("update")) {
writeModels.add(new UpdateManyModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("update"), getUpdateOptions(requestArguments)));
} else {
// update is a pipeline
writeModels.add(new UpdateManyModel<BsonDocument>(requestArguments.getDocument("filter"), getListOfDocuments(requestArguments.getArray("update")), getUpdateOptions(requestArguments)));
}
} else if (name.equals("deleteOne")) {
writeModels.add(new DeleteOneModel<BsonDocument>(requestArguments.getDocument("filter"), getDeleteOptions(requestArguments)));
} else if (name.equals("deleteMany")) {
writeModels.add(new DeleteManyModel<BsonDocument>(requestArguments.getDocument("filter"), getDeleteOptions(requestArguments)));
} else if (name.equals("replaceOne")) {
writeModels.add(new ReplaceOneModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("replacement"), getReplaceOptions(requestArguments)));
} else {
throw new UnsupportedOperationException(format("Unsupported write request type: %s", name));
}
}
try {
BulkWriteResult bulkWriteResult;
BsonDocument optionsDocument = arguments.getDocument("options", new BsonDocument());
BulkWriteOptions options = new BulkWriteOptions().ordered(optionsDocument.getBoolean("ordered", BsonBoolean.TRUE).getValue());
if (optionsDocument.containsKey("bypassDocumentValidation")) {
options.bypassDocumentValidation(optionsDocument.getBoolean("bypassDocumentValidation").getValue());
}
if (clientSession == null) {
bulkWriteResult = getCollection(collectionOptions).bulkWrite(writeModels, options);
} else {
bulkWriteResult = getCollection(collectionOptions).bulkWrite(clientSession, writeModels, options);
}
return toResult(bulkWriteResult, writeModels, Collections.<BulkWriteError>emptyList());
} catch (MongoBulkWriteException e) {
BsonDocument result = toResult(e.getWriteResult(), writeModels, e.getWriteErrors());
result.put("error", BsonBoolean.TRUE);
return result;
}
}
use of com.mongodb.client.model.DeleteOneModel in project mongo-java-driver by mongodb.
the class Operations method bulkWrite.
@SuppressWarnings("unchecked")
public MixedBulkWriteOperation bulkWrite(final List<? extends WriteModel<? extends TDocument>> requests, final BulkWriteOptions options) {
notNull("requests", requests);
List<WriteRequest> writeRequests = new ArrayList<>(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.getReplaceOptions().isUpsert()).collation(replaceOneModel.getReplaceOptions().getCollation()).hint(replaceOneModel.getReplaceOptions().getHint()).hintString(replaceOneModel.getReplaceOptions().getHintString());
} else if (writeModel instanceof UpdateOneModel) {
UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel;
BsonValue update = updateOneModel.getUpdate() != null ? toBsonDocument(updateOneModel.getUpdate()) : new BsonArray(toBsonDocumentList(updateOneModel.getUpdatePipeline()));
writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), update, WriteRequest.Type.UPDATE).multi(false).upsert(updateOneModel.getOptions().isUpsert()).collation(updateOneModel.getOptions().getCollation()).arrayFilters(toBsonDocumentList(updateOneModel.getOptions().getArrayFilters())).hint(updateOneModel.getOptions().getHint()).hintString(updateOneModel.getOptions().getHintString());
} else if (writeModel instanceof UpdateManyModel) {
UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
BsonValue update = updateManyModel.getUpdate() != null ? toBsonDocument(updateManyModel.getUpdate()) : new BsonArray(toBsonDocumentList(updateManyModel.getUpdatePipeline()));
writeRequest = new UpdateRequest(toBsonDocument(updateManyModel.getFilter()), update, WriteRequest.Type.UPDATE).multi(true).upsert(updateManyModel.getOptions().isUpsert()).collation(updateManyModel.getOptions().getCollation()).arrayFilters(toBsonDocumentList(updateManyModel.getOptions().getArrayFilters())).hint(updateManyModel.getOptions().getHint()).hintString(updateManyModel.getOptions().getHintString());
} else if (writeModel instanceof DeleteOneModel) {
DeleteOneModel<TDocument> deleteOneModel = (DeleteOneModel<TDocument>) writeModel;
writeRequest = new DeleteRequest(toBsonDocument(deleteOneModel.getFilter())).multi(false).collation(deleteOneModel.getOptions().getCollation()).hint(deleteOneModel.getOptions().getHint()).hintString(deleteOneModel.getOptions().getHintString());
} else if (writeModel instanceof DeleteManyModel) {
DeleteManyModel<TDocument> deleteManyModel = (DeleteManyModel<TDocument>) writeModel;
writeRequest = new DeleteRequest(toBsonDocument(deleteManyModel.getFilter())).multi(true).collation(deleteManyModel.getOptions().getCollation()).hint(deleteManyModel.getOptions().getHint()).hintString(deleteManyModel.getOptions().getHintString());
} else {
throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));
}
writeRequests.add(writeRequest);
}
return new MixedBulkWriteOperation(namespace, writeRequests, options.isOrdered(), writeConcern, retryWrites).bypassDocumentValidation(options.getBypassDocumentValidation());
}
Aggregations