Example usage for com.mongodb.client.model Updates setOnInsert

List of usage examples for com.mongodb.client.model Updates setOnInsert

Introduction

In this page you can find the example usage for com.mongodb.client.model Updates setOnInsert.

Prototype

public static <TItem> Bson setOnInsert(final String fieldName, @Nullable final TItem value) 

Source Link

Document

Creates an update that sets the value of the field with the given name to the given value, but only if the update is an upsert that results in an insert of a document.

Usage

From source file:com.exorath.exodata.impl.IExoDocument.java

License:Apache License

@Override
public Observable<Document> fetch(Bson projection) {

    return Observable.create((subscriber -> {
        FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().upsert(true)
                .returnDocument(ReturnDocument.AFTER).projection(projection);
        document = (Document) collection.findOneAndUpdate(getIdQuery(),
                Updates.setOnInsert("_id", getId().toString()), options);
        if (document != null)
            subscriber.onNext(document);
        subscriber.onCompleted();/*from w w  w. j  a va 2 s.  com*/
    })).subscribeOn(Schedulers.io()).cast(Document.class);
}