use of com.mongodb.bulk.InsertRequest in project mongo-java-driver by mongodb.
the class MongoCollectionImpl method insertMany.
@Override
public void insertMany(final List<? extends TDocument> documents, final InsertManyOptions options, final SingleResultCallback<Void> callback) {
notNull("documents", documents);
List<InsertRequest> requests = new ArrayList<InsertRequest>(documents.size());
for (TDocument document : documents) {
if (document == null) {
throw new IllegalArgumentException("documents can not contain a null value");
}
if (getCodec() instanceof CollectibleCodec) {
document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
}
requests.add(new InsertRequest(documentToBsonDocument(document)));
}
executor.execute(new MixedBulkWriteOperation(namespace, requests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()), errorHandlingCallback(new SingleResultCallback<BulkWriteResult>() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
callback.onResult(null, t);
}
}, LOGGER));
}
use of com.mongodb.bulk.InsertRequest 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.bulk.InsertRequest in project mongo-java-driver by mongodb.
the class DBCollection method insert.
/**
* <p>Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document
* does not contain an '_id' field, it will be added.</p>
*
* <p>If the value of the continueOnError property of the given {@code InsertOptions} is true,
* that value will override the value of the continueOnError property of the given {@code WriteConcern}. Otherwise,
* the value of the continueOnError property of the given {@code WriteConcern} will take effect. </p>
*
* @param documents a list of {@code DBObject}'s to be inserted
* @param insertOptions the options to use for the insert
* @return the result of the operation
* @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key
* @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command
* @throws MongoException if the operation failed for some other reason
* @mongodb.driver.manual tutorial/insert-documents/ Insert Documents
*/
public WriteResult insert(final List<? extends DBObject> documents, final InsertOptions insertOptions) {
WriteConcern writeConcern = insertOptions.getWriteConcern() != null ? insertOptions.getWriteConcern() : getWriteConcern();
Encoder<DBObject> encoder = toEncoder(insertOptions.getDbEncoder());
List<InsertRequest> insertRequestList = new ArrayList<InsertRequest>(documents.size());
for (DBObject cur : documents) {
if (cur.get(ID_FIELD_NAME) == null) {
cur.put(ID_FIELD_NAME, new ObjectId());
}
insertRequestList.add(new InsertRequest(new BsonDocumentWrapper<DBObject>(cur, encoder)));
}
return insert(insertRequestList, writeConcern, insertOptions.isContinueOnError(), insertOptions.getBypassDocumentValidation());
}
use of com.mongodb.bulk.InsertRequest 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.bulk.InsertRequest in project mongo-java-driver by mongodb.
the class MongoCollectionImpl method insertMany.
@Override
public void insertMany(final List<? extends TDocument> documents, final InsertManyOptions options) {
notNull("documents", documents);
List<InsertRequest> requests = new ArrayList<InsertRequest>(documents.size());
for (TDocument document : documents) {
if (document == null) {
throw new IllegalArgumentException("documents can not contain a null value");
}
if (getCodec() instanceof CollectibleCodec) {
document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
}
requests.add(new InsertRequest(documentToBsonDocument(document)));
}
executor.execute(new MixedBulkWriteOperation(namespace, requests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()));
}
Aggregations