Spring webflux ReactiveMongoOperations ค้นหาโดย 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วิธีการทำงาน เนื่องจากการสมัครใช้งานทำงานเป็นการดำเนินการแบบอะซิงโครนัสเมื่อคุณรันสิ่งนี้จะเพิ่มสิ่งต่อไปนี้ในเธรดปัจจุบัน (สำหรับการบล็อกจนกว่าการดำเนินการ async จะเสร็จสิ้น)
try {
Thread.sleep(1000);
} catch(InterruptedException e ) {}