Search in sources :

Example 1 with DeleteManyModel

use of com.mongodb.client.model.DeleteManyModel 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);
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) CollectibleCodec(org.bson.codecs.CollectibleCodec) UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) InsertRequest(com.mongodb.bulk.InsertRequest) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) MixedBulkWriteOperation(com.mongodb.operation.MixedBulkWriteOperation) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) DeleteRequest(com.mongodb.bulk.DeleteRequest) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Example 2 with DeleteManyModel

use of com.mongodb.client.model.DeleteManyModel 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()));
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) CollectibleCodec(org.bson.codecs.CollectibleCodec) UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) InsertRequest(com.mongodb.bulk.InsertRequest) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) MixedBulkWriteOperation(com.mongodb.operation.MixedBulkWriteOperation) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) DeleteRequest(com.mongodb.bulk.DeleteRequest) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Example 3 with DeleteManyModel

use of com.mongodb.client.model.DeleteManyModel 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());
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) CollectibleCodec(org.bson.codecs.CollectibleCodec) UpdateRequest(com.mongodb.internal.bulk.UpdateRequest) WriteRequest(com.mongodb.internal.bulk.WriteRequest) InsertRequest(com.mongodb.internal.bulk.InsertRequest) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BsonArray(org.bson.BsonArray) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) DeleteRequest(com.mongodb.internal.bulk.DeleteRequest) BsonValue(org.bson.BsonValue) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Example 4 with DeleteManyModel

use of com.mongodb.client.model.DeleteManyModel in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method toWriteModel.

private WriteModel<BsonDocument> toWriteModel(final BsonDocument document) {
    String requestType = document.getFirstKey();
    BsonDocument arguments = document.getDocument(requestType);
    switch(requestType) {
        case "insertOne":
            return new InsertOneModel<>(arguments.getDocument("document"));
        case "updateOne":
            return arguments.isArray("update") ? new UpdateOneModel<>(arguments.getDocument("filter"), arguments.getArray("update").stream().map(BsonValue::asDocument).collect(toList()), getUpdateOptions(arguments)) : new UpdateOneModel<>(arguments.getDocument("filter"), arguments.getDocument("update"), getUpdateOptions(arguments));
        case "updateMany":
            return arguments.isArray("update") ? new UpdateManyModel<>(arguments.getDocument("filter"), arguments.getArray("update").stream().map(BsonValue::asDocument).collect(toList()), getUpdateOptions(arguments)) : new UpdateManyModel<>(arguments.getDocument("filter"), arguments.getDocument("update"), getUpdateOptions(arguments));
        case "deleteOne":
            return new DeleteOneModel<>(arguments.getDocument("filter"), getDeleteOptions(arguments));
        case "deleteMany":
            return new DeleteManyModel<>(arguments.getDocument("filter"), getDeleteOptions(arguments));
        case "replaceOne":
            return new ReplaceOneModel<>(arguments.getDocument("filter"), arguments.getDocument("replacement"), getReplaceOptions(arguments));
        default:
            throw new UnsupportedOperationException("Unsupported write model type: " + requestType);
    }
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) BsonDocument(org.bson.BsonDocument) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) BsonString(org.bson.BsonString) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Example 5 with DeleteManyModel

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

the class DefaultBulkOperationsUnitTests method bulkRemoveShouldMapQueryCorrectly.

// DATAMONGO-1678
@Test
void bulkRemoveShouldMapQueryCorrectly() {
    ops.remove(query(where("firstName").is("danerys"))).execute();
    verify(collection).bulkWrite(captor.capture(), any());
    DeleteManyModel<Document> updateModel = (DeleteManyModel<Document>) captor.getValue().get(0);
    assertThat(updateModel.getFilter()).isEqualTo(new Document("first_name", "danerys"));
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DeleteManyModel(com.mongodb.client.model.DeleteManyModel) Test(org.junit.jupiter.api.Test)

Aggregations

DeleteManyModel (com.mongodb.client.model.DeleteManyModel)5 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)4 InsertOneModel (com.mongodb.client.model.InsertOneModel)4 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)4 UpdateManyModel (com.mongodb.client.model.UpdateManyModel)3 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)3 ArrayList (java.util.ArrayList)3 CollectibleCodec (org.bson.codecs.CollectibleCodec)3 DeleteRequest (com.mongodb.bulk.DeleteRequest)2 InsertRequest (com.mongodb.bulk.InsertRequest)2 UpdateRequest (com.mongodb.bulk.UpdateRequest)2 WriteRequest (com.mongodb.bulk.WriteRequest)2 MixedBulkWriteOperation (com.mongodb.operation.MixedBulkWriteOperation)2 BsonDocument (org.bson.BsonDocument)2 DeleteRequest (com.mongodb.internal.bulk.DeleteRequest)1 InsertRequest (com.mongodb.internal.bulk.InsertRequest)1 UpdateRequest (com.mongodb.internal.bulk.UpdateRequest)1 WriteRequest (com.mongodb.internal.bulk.WriteRequest)1 BsonArray (org.bson.BsonArray)1 BsonString (org.bson.BsonString)1