Spring webflux ReactiveMongoOperations trova da elemMatch

Oct 05 2020

Ho una collezione come questa:

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

e voglio trovare INTERLOCUTOR avere test1 ; come si usa da ReactiveMongoOperations ?

Risposte

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

Utilizzo ReactiveMongoOperationsed elaborazione del restituito reactor.core.publisher.Fluxper stampare i documenti restituiti dalla query:

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

Notare che la query viene effettivamente eseguita quando viene eseguito il subscribemetodo. Poiché la sottoscrizione viene eseguita come operazione asincrona, quando si esegue questa operazione aggiungere quanto segue al thread corrente (per bloccarlo fino al completamento dell'operazione asincrona).

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