Spring Webflux ReactiveMongoOperations finden von elemMatch

Oct 05 2020

Ich habe eine Sammlung wie diese:

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

und ich möchte finden ANSPRECHPARTNER haben test1 ; Wie verwende ich ReactiveMongoOperations ?

Antworten

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

Verwenden ReactiveMongoOperationsund Verarbeiten der reactor.core.publisher.Fluxzurückgegebenen Dokumente zum Drucken der zurückgegebenen Abfragedokumente:

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

Beachten Sie, dass die Abfrage tatsächlich ausgeführt wird, wenn die subscribeMethode ausgeführt wird. Da das Abonnement als asynchrone Operation ausgeführt wird, fügen Sie dem aktuellen Thread Folgendes hinzu (um es zu blockieren, bis die asynchrone Operation abgeschlossen ist).

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