下面列出了怎么用com.mongodb.reactivestreams.client.FindPublisher的API类实例代码及写法,或者点击链接到github查看源代码。
@Bean
public Action<Chain> home() {
return chain -> chain.get(ctx -> {
FindPublisher<News> databasePublisher =
databaseNews().lookupNews();
Observable<News> httpNewsObservable =
externalNews().retrieveNews();
TransformablePublisher<News> stream = Streams.merge(
databasePublisher,
RxReactiveStreams.toPublisher(httpNewsObservable)
);
ctx.render(
stream.toList()
.map(Jackson::json)
);
});
}
@Override
public void subscribe(Subscriber<? super News> s) {
FindPublisher<News> findPublisher = collection.find(News.class);
findPublisher.sort(Sorts.descending("publishedOn"))
.filter(Filters.and(
Filters.eq("category", category),
Filters.gt("publishedOn", today())
))
.subscribe(s);
}
private Source<Document, NotUsed> findAllInternal(final Query query, final List<String> authorizationSubjectIds,
@Nullable final Set<String> namespaces,
@Nullable final Integer limit,
@Nullable final Duration maxQueryTime) {
checkNotNull(query, "query");
final BsonDocument queryFilter = getMongoFilter(query, authorizationSubjectIds);
if (log.isDebugEnabled()) {
log.debug("findAll with query filter <{}>.", queryFilter);
}
final Bson sortOptions = getMongoSort(query);
final int skip = query.getSkip();
final Bson projection = GetSortBsonVisitor.projections(query.getSortOptions());
final FindPublisher<Document> findPublisher =
collection.find(queryFilter, Document.class)
.hint(hints.getHint(namespaces).orElse(null))
.sort(sortOptions)
.skip(skip)
.projection(projection);
final FindPublisher<Document> findPublisherWithLimit = limit != null
? findPublisher.limit(limit)
: findPublisher;
final FindPublisher<Document> findPublisherWithMaxQueryTime = maxQueryTime != null
? findPublisherWithLimit.maxTime(maxQueryTime.getSeconds(), TimeUnit.SECONDS)
: findPublisherWithLimit;
return Source.fromPublisher(findPublisherWithMaxQueryTime);
}
@Test
public void testQueryingNullandMissingFields() throws Throwable {
//Start Example 38
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{'_id': 1, 'item': null}"),
Document.parse("{'_id': 2}")
));
// End Example 38
assertSuccess(insertManyPublisher);
assertSize(collection.find(), 2);
//Start Example 39
FindPublisher<Document> findPublisher = collection.find(eq("item", null));
//End Example 39
assertSize(findPublisher, 2);
//Start Example 40
findPublisher = collection.find(type("item", BsonType.NULL));
//End Example 40
assertSize(findPublisher, 1);
//Start Example 41
findPublisher = collection.find(exists("item", false));
//End Example 41
assertSize(findPublisher, 1);
}
public <T> FindPublisher<T> apply(FindPublisher<T> stream) {
FindPublisher<T> publisher = stream;
if (filter != null) {
publisher = publisher.filter(filter);
}
if (limit > 0) {
publisher = publisher.limit(limit);
}
if (skip > 0) {
publisher = publisher.skip(skip);
}
if (maxTime > 0) {
publisher = publisher.maxTime(maxTime, maxTimeUnit);
}
if (maxAwaitTime > 0) {
publisher = publisher.maxAwaitTime(maxAwaitTime, maxAwaitTimeUnit);
}
if (projection != null) {
publisher = publisher.projection(projection);
}
if (sort != null) {
publisher = publisher.sort(sort);
}
if (noCursorTimeout) {
publisher = publisher.noCursorTimeout(true);
}
if (oplogReplay) {
publisher = publisher.oplogReplay(true);
}
if (partial) {
publisher = publisher.partial(true);
}
if (cursorType != null) {
publisher = publisher.cursorType(cursorType);
}
if (collation != null) {
publisher = publisher.collation(collation);
}
if (comment != null) {
publisher = publisher.comment(comment);
}
if (hint != null) {
publisher = publisher.hint(hint);
}
if (max != null) {
publisher = publisher.max(max);
}
if (min != null) {
publisher = publisher.min(min);
}
if (returnKey) {
publisher = publisher.returnKey(true);
}
if (showRecordId) {
publisher = publisher.showRecordId(true);
}
return publisher;
}
private <D> FindPublisher<D> apply(FindOptions options, FindPublisher<D> publisher) {
if (options == null) {
return publisher;
}
return options.apply(publisher);
}
@Test
public void testInsert() throws Throwable {
// Start Example 1
Document canvas = new Document("item", "canvas")
.append("qty", 100)
.append("tags", singletonList("cotton"));
Document size = new Document("h", 28)
.append("w", 35.5)
.append("uom", "cm");
canvas.put("size", size);
Publisher<Success> insertOnePublisher = collection.insertOne(canvas);
// End Example 1
assertSuccess(insertOnePublisher);
// Start Example 2
FindPublisher<Document> findPublisher = collection.find(eq("item", "canvas"));
// End Example 2
assertSize(findPublisher, 1);
// Start Example 3
Document journal = new Document("item", "journal")
.append("qty", 25)
.append("tags", asList("blank", "red"));
Document journalSize = new Document("h", 14)
.append("w", 21)
.append("uom", "cm");
journal.put("size", journalSize);
Document mat = new Document("item", "mat")
.append("qty", 85)
.append("tags", singletonList("gray"));
Document matSize = new Document("h", 27.9)
.append("w", 35.5)
.append("uom", "cm");
mat.put("size", matSize);
Document mousePad = new Document("item", "mousePad")
.append("qty", 25)
.append("tags", asList("gel", "blue"));
Document mousePadSize = new Document("h", 19)
.append("w", 22.85)
.append("uom", "cm");
mousePad.put("size", mousePadSize);
Publisher<Success> insertManyPublisher = collection.insertMany(asList(journal, mat, mousePad));
// End Example 3
assertSuccess(insertManyPublisher);
assertSize(collection.find(), 4);
}
@Test
public void testQueryingAtTheTopLevel() throws Throwable {
// Start Example 6
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"),
Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }")
));
// End Example 6
assertSuccess(insertManyPublisher);
assertSize(collection.find(), 5);
// Start Example 7
FindPublisher<Document> findPublisher = collection.find(new Document());
// End Example 7
assertSize(findPublisher, 5);
// Start Example 8
findPublisher = collection.find();
// End Example 8
assertSize(findPublisher, 5);
// Start Example 9
findPublisher = collection.find(eq("status", "D"));
// End Example 9
assertSize(findPublisher, 2);
// Start Example 10
findPublisher = collection.find(in("status", "A", "D"));
// End Example 10
assertSize(findPublisher, 5);
// Start Example 11
findPublisher = collection.find(and(eq("status", "A"), lt("qty", 30)));
// End Example 11
assertSize(findPublisher, 1);
// Start Example 12
findPublisher = collection.find(or(eq("status", "A"), lt("qty", 30)));
// End Example 12
assertSize(findPublisher, 3);
// Start Example 13
findPublisher = collection.find(
and(eq("status", "A"),
or(lt("qty", 30), regex("item", "^p")))
);
// End Example 13
assertSize(findPublisher, 2);
}
@Test
public void testQueryingEmbeddedDocuments() throws Throwable {
// Start Example 14
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"),
Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }")
));
// End Example 14
assertSuccess(insertManyPublisher);
assertSize(collection.find(), 5);
// Start Example 15
FindPublisher<Document> findPublisher = collection.find(eq("size", Document.parse("{ h: 14, w: 21, uom: 'cm' }")));
// End Example 15
assertSize(findPublisher, 1);
// Start Example 16
findPublisher = collection.find(eq("size", Document.parse("{ w: 21, h: 14, uom: 'cm' }")));
// End Example 16
assertSize(findPublisher, 0);
// Start Example 17
findPublisher = collection.find(eq("size.uom", "in"));
// End Example 17
assertSize(findPublisher, 2);
// Start Example 18
findPublisher = collection.find(lt("size.h", 15));
// End Example 18
assertSize(findPublisher, 4);
// Start Example 19
findPublisher = collection.find(and(
lt("size.h", 15),
eq("size.uom", "in"),
eq("status", "D")
));
// End Example 19
assertSize(findPublisher, 1);
}
@Test
public void testQueryingArrayValues() throws Throwable {
//Start Example 20
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{ item: 'journal', qty: 25, tags: ['blank', 'red'], dim_cm: [ 14, 21 ] }"),
Document.parse("{ item: 'notebook', qty: 50, tags: ['red', 'blank'], dim_cm: [ 14, 21 ] }"),
Document.parse("{ item: 'paper', qty: 100, tags: ['red', 'blank', 'plain'], dim_cm: [ 14, 21 ] }"),
Document.parse("{ item: 'planner', qty: 75, tags: ['blank', 'red'], dim_cm: [ 22.85, 30 ] }"),
Document.parse("{ item: 'postcard', qty: 45, tags: ['blue'], dim_cm: [ 10, 15.25 ] }")
));
// End Example 20
assertSuccess(insertManyPublisher);
assertSize(collection.find(), 5);
//Start Example 21
FindPublisher<Document> findPublisher = collection.find(eq("tags", asList("red", "blank")));
//End Example 21
assertSize(findPublisher, 1);
//Start Example 22
findPublisher = collection.find(all("tags", asList("red", "blank")));
//End Example 22
assertSize(findPublisher, 4);
//Start Example 23
findPublisher = collection.find(eq("tags", "red"));
//End Example 23
assertSize(findPublisher, 4);
//Start Example 24
findPublisher = collection.find(gt("dim_cm", 25));
//End Example 24
assertSize(findPublisher, 1);
//Start Example 25
findPublisher = collection.find(and(gt("dim_cm", 15), lt("dim_cm", 20)));
//End Example 25
assertSize(findPublisher, 4);
//Start Example 26
findPublisher = collection.find(elemMatch("dim_cm", Document.parse("{ $gt: 22, $lt: 30 }")));
//End Example 26
assertSize(findPublisher, 1);
//Start Example 27
findPublisher = collection.find(gt("dim_cm.1", 25));
//End Example 27
assertSize(findPublisher, 1);
//Start Example 28
findPublisher = collection.find(size("tags", 3));
//End Example 28
assertSize(findPublisher, 1);
}
@Test
public void testQueryingArraysContainingDocuments() throws Throwable {
//Start Example 29
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{ item: 'journal', instock: [ { warehouse: 'A', qty: 5 }, { warehouse: 'C', qty: 15 } ] }"),
Document.parse("{ item: 'notebook', instock: [ { warehouse: 'C', qty: 5 } ] }"),
Document.parse("{ item: 'paper', instock: [ { warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 15 } ] }"),
Document.parse("{ item: 'planner', instock: [ { warehouse: 'A', qty: 40 }, { warehouse: 'B', qty: 5 } ] }"),
Document.parse("{ item: 'postcard', instock: [ { warehouse: 'B', qty: 15 }, { warehouse: 'C', qty: 35 } ] }")
));
// End Example 29
assertSuccess(insertManyPublisher);
assertSize(collection.find(), 5);
//Start Example 30
FindPublisher<Document> findPublisher = collection.find(eq("instock", Document.parse("{ warehouse: 'A', qty: 5 }")));
//End Example 30
assertSize(findPublisher, 1);
//Start Example 31
findPublisher = collection.find(eq("instock", Document.parse("{ qty: 5, warehouse: 'A' }")));
//End Example 31
assertSize(findPublisher, 0);
//Start Example 32
findPublisher = collection.find(lte("instock.0.qty", 20));
//End Example 32
assertSize(findPublisher, 3);
//Start Example 33
findPublisher = collection.find(lte("instock.qty", 20));
//End Example 33
assertSize(findPublisher, 5);
//Start Example 34
findPublisher = collection.find(elemMatch("instock", Document.parse("{ qty: 5, warehouse: 'A' }")));
//End Example 34
assertSize(findPublisher, 1);
//Start Example 35
findPublisher = collection.find(elemMatch("instock", Document.parse("{ qty: { $gt: 10, $lte: 20 } }")));
//End Example 35
assertSize(findPublisher, 3);
//Start Example 36
findPublisher = collection.find(and(gt("instock.qty", 10), lte("instock.qty", 20)));
//End Example 36
assertSize(findPublisher, 4);
//Start Example 37
findPublisher = collection.find(and(eq("instock.qty", 5), eq("instock.warehouse", "A")));
//End Example 37
assertSize(findPublisher, 2);
}
@Override
public FindPublisher<TDocument> find() {
return find(new BsonDocument(), getDocumentClass());
}
@Override
public <TResult> FindPublisher<TResult> find(final Class<TResult> clazz) {
return find(new BsonDocument(), clazz);
}
@Override
public FindPublisher<TDocument> find(final Bson filter) {
return find(filter, getDocumentClass());
}
@Override
public <TResult> FindPublisher<TResult> find(final Bson filter, final Class<TResult> clazz) {
return new FindPublisherImpl<TResult>(wrapped.find(filter, clazz));
}
@Override
public FindPublisher<TDocument> find(final ClientSession clientSession) {
return find(clientSession, new BsonDocument(), getDocumentClass());
}
@Override
public <TResult> FindPublisher<TResult> find(final ClientSession clientSession, final Class<TResult> clazz) {
return find(clientSession, new BsonDocument(), clazz);
}
@Override
public FindPublisher<TDocument> find(final ClientSession clientSession, final Bson filter) {
return find(clientSession, filter, getDocumentClass());
}
@Override
public <TResult> FindPublisher<TResult> find(final ClientSession clientSession, final Bson filter, final Class<TResult> clazz) {
return new FindPublisherImpl<TResult>(wrapped.find(clientSession.getWrapped(), filter, clazz));
}
@Override
public FindPublisher<TResult> hintString(final String hint) {
wrapped.hintString(hint);
return this;
}
FindPublisher<News> lookupNews();