Spring webflux ReactiveMongoOperations được tìm thấy bởi elemMatch
Oct 05 2020
Tôi có một bộ sưu tập như thế này:
{"type": "bbb", "category": "aaa", "from": "eee", "INTERLOCUTOR": ["test1", "test2"]}
và tôi muốn tìm INTERLOCUTOR có test1 ; làm thế nào để sử dụng bởi ReactiveMongoOperations ?
Trả lời
2 Tahero Oct 05 2020 at 10:09
db.collection.find({"INTERLOCUTOR" : "test1"})
2 prasad_ Oct 05 2020 at 11:37
Sử dụng ReactiveMongoOperationsvà xử lý reactor.core.publisher.Fluxtài liệu trả về để in tài liệu được trả về truy vấn:
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());
Lưu ý rằng truy vấn thực sự thực thi khi subscribephương thức chạy. Vì đăng ký chạy như một hoạt động không đồng bộ, khi bạn chạy điều này, hãy thêm phần sau vào luồng hiện tại (để chặn nó cho đến khi hoạt động không đồng bộ hoàn tất).
try {
Thread.sleep(1000);
} catch(InterruptedException e ) {}