Example usage for com.mongodb BulkWriteRequestBuilder upsert

List of usage examples for com.mongodb BulkWriteRequestBuilder upsert

Introduction

In this page you can find the example usage for com.mongodb BulkWriteRequestBuilder upsert.

Prototype

public BulkUpdateRequestBuilder upsert() 

Source Link

Document

Specifies that the request being built should be an upsert.

Usage

From source file:org.springframework.data.mongodb.core.DefaultBulkOperations.java

License:Apache License

/**
 * Performs update and upsert bulk operations.
 * // ww  w . ja  va2 s.co m
 * @param query the {@link Query} to determine documents to update.
 * @param update the {@link Update} to perform, must not be {@literal null}.
 * @param upsert whether to upsert.
 * @param multi whether to issue a multi-update.
 * @return the {@link BulkOperations} with the update registered.
 */
private BulkOperations update(Query query, Update update, boolean upsert, boolean multi) {

    Assert.notNull(query, "Query must not be null!");
    Assert.notNull(update, "Update must not be null!");

    BulkWriteRequestBuilder builder = bulk.find(query.getQueryObject());

    if (upsert) {

        if (multi) {
            builder.upsert().update(update.getUpdateObject());
        } else {
            builder.upsert().updateOne(update.getUpdateObject());
        }

    } else {

        if (multi) {
            builder.update(update.getUpdateObject());
        } else {
            builder.updateOne(update.getUpdateObject());
        }
    }

    return this;
}