Search in sources :

Example 6 with CollectibleCodec

use of org.bson.codecs.CollectibleCodec in project mongo-java-driver by mongodb.

the class MongoCollectionImpl method insertOne.

@Override
public void insertOne(final TDocument document, final InsertOneOptions options, final SingleResultCallback<Void> callback) {
    TDocument insertDocument = document;
    if (getCodec() instanceof CollectibleCodec) {
        insertDocument = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(insertDocument);
    }
    executeSingleWriteRequest(new InsertRequest(documentToBsonDocument(insertDocument)), options.getBypassDocumentValidation(), new SingleResultCallback<BulkWriteResult>() {

        @Override
        public void onResult(final BulkWriteResult result, final Throwable t) {
            callback.onResult(null, t);
        }
    });
}
Also used : CollectibleCodec(org.bson.codecs.CollectibleCodec) InsertRequest(com.mongodb.bulk.InsertRequest) BulkWriteResult(com.mongodb.bulk.BulkWriteResult)

Example 7 with CollectibleCodec

use of org.bson.codecs.CollectibleCodec in project mongo-java-driver by mongodb.

the class MongoCollectionImpl method insertOne.

@Override
public void insertOne(final TDocument document, final InsertOneOptions options) {
    notNull("document", document);
    TDocument insertDocument = document;
    if (getCodec() instanceof CollectibleCodec) {
        insertDocument = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
    }
    executeSingleWriteRequest(new InsertRequest(documentToBsonDocument(insertDocument)), options.getBypassDocumentValidation());
}
Also used : CollectibleCodec(org.bson.codecs.CollectibleCodec) InsertRequest(com.mongodb.bulk.InsertRequest)

Example 8 with CollectibleCodec

use of org.bson.codecs.CollectibleCodec 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)

Aggregations

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