Spring Webflux ReactiveMongoOperations find by elemMatch

Oct 05 2020

다음과 같은 컬렉션이 있습니다.

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

INTERLOCUTOR 가 test1을 갖고 있는지 찾고 싶습니다 . ReactiveMongoOperations 에서 사용하는 방법 ?

답변

2 Tahero Oct 05 2020 at 10:09
db.collection.find({"INTERLOCUTOR" : "test1"})
2 prasad_ Oct 05 2020 at 11:37

ReactiveMongoOperations반환 된 reactor.core.publisher.Flux것을 사용 하고 처리하여 쿼리 반환 된 문서를 인쇄합니다.

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());

subscribe메서드가 실행될 때 쿼리가 실제로 실행 됩니다. 구독은 비동기 작업으로 실행되므로이를 실행할 때 현재 스레드에 다음을 추가합니다 (비동기 작업이 완료 될 때까지 차단하기 위해).

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