1

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?

J. Hu
  • 53
  • 8

2 Answers2

2
db.collection.find({"INTERLOCUTOR" : "test1"})
Tahero
  • 328
  • 4
  • 11
2

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 ) {}
prasad_
  • 12,755
  • 2
  • 24
  • 36
  • @prased_ it might be a silly question but I really want to understand it: if I have to block for 1 second until the async subscribe complete what is the advantage to use ReactiveMongoOperations? – Jim C Nov 16 '20 at 18:01
  • @JimC Actual applications don't run 5 lines of code in the `main` method and end the application after querying a database's collection. There is more to an application - this is just to demonstrate the basic usage of the API and its syntax. The `Flux` (and also see `Mono`) class has methods to work with various scenarios (including blocking methods) and you can use the ones that are suitable for your application. – prasad_ Nov 17 '20 at 02:36