Example usage for com.mongodb.client ChangeStreamIterable fullDocument

List of usage examples for com.mongodb.client ChangeStreamIterable fullDocument

Introduction

In this page you can find the example usage for com.mongodb.client ChangeStreamIterable fullDocument.

Prototype

ChangeStreamIterable<TResult> fullDocument(FullDocument fullDocument);

Source Link

Document

Sets the fullDocument value.

Usage

From source file:org.springframework.data.mongodb.core.messaging.ChangeStreamTask.java

License:Apache License

@Override
protected MongoCursor<ChangeStreamDocument<Document>> initCursor(MongoTemplate template, RequestOptions options,
        Class<?> targetType) {

    List<Document> filter = Collections.emptyList();
    BsonDocument resumeToken = new BsonDocument();
    Collation collation = null;//from  ww w . j ava 2s.  com
    FullDocument fullDocument = FullDocument.DEFAULT;

    if (options instanceof ChangeStreamRequest.ChangeStreamRequestOptions) {

        ChangeStreamOptions changeStreamOptions = ((ChangeStreamRequestOptions) options)
                .getChangeStreamOptions();
        filter = prepareFilter(template, changeStreamOptions);

        if (changeStreamOptions.getFilter().isPresent()) {

            Object val = changeStreamOptions.getFilter().get();
            if (val instanceof Aggregation) {
                collation = ((Aggregation) val).getOptions().getCollation()
                        .map(org.springframework.data.mongodb.core.query.Collation::toMongoCollation)
                        .orElse(null);
            }
        }

        if (changeStreamOptions.getResumeToken().isPresent()) {
            resumeToken = changeStreamOptions.getResumeToken().get().asDocument();
        }

        fullDocument = changeStreamOptions.getFullDocumentLookup()
                .orElseGet(() -> ClassUtils.isAssignable(Document.class, targetType) ? FullDocument.DEFAULT
                        : FullDocument.UPDATE_LOOKUP);
    }

    ChangeStreamIterable<Document> iterable = filter.isEmpty()
            ? template.getCollection(options.getCollectionName()).watch(Document.class)
            : template.getCollection(options.getCollectionName()).watch(filter, Document.class);

    if (!resumeToken.isEmpty()) {
        iterable = iterable.resumeAfter(resumeToken);
    }

    if (collation != null) {
        iterable = iterable.collation(collation);
    }

    iterable = iterable.fullDocument(fullDocument);

    return iterable.iterator();
}