Search in sources :

Example 6 with UpdateRequest

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

the class OperationHelper method validateWriteRequestCollations.

static void validateWriteRequestCollations(final AsyncConnection connection, final List<? extends WriteRequest> requests, final WriteConcern writeConcern, final AsyncCallableWithConnection callable) {
    Collation collation = null;
    for (WriteRequest request : requests) {
        if (request instanceof UpdateRequest) {
            collation = ((UpdateRequest) request).getCollation();
        } else if (request instanceof DeleteRequest) {
            collation = ((DeleteRequest) request).getCollation();
        }
        if (collation != null) {
            break;
        }
    }
    validateCollationAndWriteConcern(connection, collation, writeConcern, new AsyncCallableWithConnection() {

        @Override
        public void call(final AsyncConnection connection, final Throwable t) {
            callable.call(connection, t);
        }
    });
}
Also used : UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) AsyncConnection(com.mongodb.connection.AsyncConnection) Collation(com.mongodb.client.model.Collation) DeleteRequest(com.mongodb.bulk.DeleteRequest)

Example 7 with UpdateRequest

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

the class OperationHelper method validateWriteRequestCollations.

static void validateWriteRequestCollations(final Connection connection, final List<? extends WriteRequest> requests, final WriteConcern writeConcern) {
    Collation collation = null;
    for (WriteRequest request : requests) {
        if (request instanceof UpdateRequest) {
            collation = ((UpdateRequest) request).getCollation();
        } else if (request instanceof DeleteRequest) {
            collation = ((DeleteRequest) request).getCollation();
        }
        if (collation != null) {
            break;
        }
    }
    validateCollationAndWriteConcern(connection, collation, writeConcern);
}
Also used : UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) Collation(com.mongodb.client.model.Collation) DeleteRequest(com.mongodb.bulk.DeleteRequest)

Example 8 with UpdateRequest

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

the class UpdateCommandMessage method writeTheWrites.

@Override
protected UpdateCommandMessage writeTheWrites(final BsonOutput bsonOutput, final int commandStartPosition, final BsonBinaryWriter writer) {
    UpdateCommandMessage nextMessage = null;
    writer.writeStartArray("updates");
    for (int i = 0; i < updates.size(); i++) {
        writer.mark();
        UpdateRequest update = updates.get(i);
        writer.writeStartDocument();
        writer.pushMaxDocumentSize(getSettings().getMaxDocumentSize());
        writer.writeName("q");
        getCodec(update.getFilter()).encode(writer, update.getFilter(), EncoderContext.builder().build());
        writer.writeName("u");
        int bufferPosition = bsonOutput.getPosition();
        getCodec(update.getUpdate()).encode(writer, update.getUpdate(), EncoderContext.builder().build());
        if (update.getType() == WriteRequest.Type.UPDATE && bsonOutput.getPosition() == bufferPosition + 8) {
            throw new IllegalArgumentException("Invalid BSON document for an update");
        }
        if (update.isMulti()) {
            writer.writeBoolean("multi", update.isMulti());
        }
        if (update.isUpsert()) {
            writer.writeBoolean("upsert", update.isUpsert());
        }
        if (update.getCollation() != null) {
            writer.writeName("collation");
            BsonDocument collation = update.getCollation().asDocument();
            getCodec(collation).encode(writer, collation, EncoderContext.builder().build());
        }
        writer.popMaxDocumentSize();
        writer.writeEndDocument();
        if (exceedsLimits(bsonOutput.getPosition() - commandStartPosition, i + 1)) {
            writer.reset();
            nextMessage = new UpdateCommandMessage(getWriteNamespace(), isOrdered(), getWriteConcern(), getBypassDocumentValidation(), getSettings(), updates.subList(i, updates.size()));
            break;
        }
    }
    writer.writeEndArray();
    return nextMessage;
}
Also used : BsonDocument(org.bson.BsonDocument) UpdateRequest(com.mongodb.bulk.UpdateRequest)

Example 9 with UpdateRequest

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

the class UpdateProtocol method appendToWriteCommandResponseDocument.

@Override
protected void appendToWriteCommandResponseDocument(final RequestMessage curMessage, final RequestMessage nextMessage, final WriteConcernResult writeConcernResult, final BsonDocument response) {
    response.append("n", new BsonInt32(writeConcernResult.getCount()));
    UpdateMessage updateMessage = (UpdateMessage) curMessage;
    UpdateRequest updateRequest = updateMessage.getUpdateRequests().get(0);
    BsonValue upsertedId = null;
    if (writeConcernResult.getUpsertedId() != null) {
        upsertedId = writeConcernResult.getUpsertedId();
    } else if (!writeConcernResult.isUpdateOfExisting() && updateRequest.isUpsert()) {
        if (updateRequest.getUpdate().containsKey("_id")) {
            upsertedId = updateRequest.getUpdate().get("_id");
        } else if (updateRequest.getFilter().containsKey("_id")) {
            upsertedId = updateRequest.getFilter().get("_id");
        }
    }
    if (upsertedId != null) {
        response.append("upserted", new BsonArray(singletonList(new BsonDocument("index", new BsonInt32(0)).append("_id", upsertedId))));
    }
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) UpdateRequest(com.mongodb.bulk.UpdateRequest) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

Aggregations

UpdateRequest (com.mongodb.bulk.UpdateRequest)9 DeleteRequest (com.mongodb.bulk.DeleteRequest)4 WriteRequest (com.mongodb.bulk.WriteRequest)4 InsertRequest (com.mongodb.bulk.InsertRequest)2 Collation (com.mongodb.client.model.Collation)2 DeleteManyModel (com.mongodb.client.model.DeleteManyModel)2 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)2 InsertOneModel (com.mongodb.client.model.InsertOneModel)2 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)2 UpdateManyModel (com.mongodb.client.model.UpdateManyModel)2 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)2 FindAndUpdateOperation (com.mongodb.operation.FindAndUpdateOperation)2 MixedBulkWriteOperation (com.mongodb.operation.MixedBulkWriteOperation)2 UpdateOperation (com.mongodb.operation.UpdateOperation)2 ArrayList (java.util.ArrayList)2 BsonDocument (org.bson.BsonDocument)2 CollectibleCodec (org.bson.codecs.CollectibleCodec)2 AsyncConnection (com.mongodb.connection.AsyncConnection)1 CollectibleDocumentFieldNameValidator (com.mongodb.internal.validator.CollectibleDocumentFieldNameValidator)1 NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)1