J. Hu
J. Hu

Reputation: 53

Spring webflux ReactiveMongoOperations find by elemMatch

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?

Upvotes: 1

Views: 891

Answers (2)

prasad_
prasad_

Reputation: 14287

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 ) {}

Upvotes: 2

Tahero
Tahero

Reputation: 328

db.collection.find({"INTERLOCUTOR" : "test1"})

Upvotes: 2

Related Questions