Spring webflux ReactiveMongoOperations find by elemMatch

天涯浪子 提交于 2020-12-13 04:41:08

问题


I have a collection like this:

{"type": "bbb", "category": "aaa", "from": "eee", "INTERLOCUTOR": ["test1", "test2"]}

and I want to find INTERLOCUTOR have test1 ; how to use by ReactiveMongoOperations?


回答1:


db.collection.find({"INTERLOCUTOR" : "test1"})



回答2:


Using ReactiveMongoOperations and processing the returned reactor.core.publisher.Flux to print the query returned documents:

ReactiveMongoOperations ops = new ReactiveMongoTemplate(MongoClients.create(), "test");
Criteria c = Criteria.where("INTERLOCUTOR").is("test1");
Query qry = new Query(c);

Flux<Document> flux = ops.find(qry, Document.class, "coll")
flux.subscribe(doc -> System.out.println(doc), throwable -> throwable.printStackTrace());

Note the query actually executes when the subscribe method runs. Since the subscribe runs as an asynchronous operation, when you run this add the following to the current thread (for blocking it until the async operation completes).

try {
  Thread.sleep(1000);
} catch(InterruptedException e ) {}


来源:https://stackoverflow.com/questions/64206481/spring-webflux-reactivemongooperations-find-by-elemmatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!