Spring webflux ReactiveMongoOperations find by elemMatch

819 views Asked by At

I have a collection like this:

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

and I want to find INTERLOCUTOR have test1 ; how to use by ReactiveMongoOperations?

2

There are 2 answers

2
prasad_ On

Using ReactiveMongoOperations and processing the returned reactor.core.publisher.Flux to print the query returned documents:

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

Note the query actually executes when the subscribe method runs. Since the subscribe runs as an asynchronous operation, when you run this add the following to the current thread (for blocking it until the async operation completes).

try {
  Thread.sleep(1000);
} catch(InterruptedException e ) {}
0
Tahero On
db.collection.find({"INTERLOCUTOR" : "test1"})