, free & fully virtual. Register Now, free & fully virtual. Register Now

How to query documents with com.mongodb.reactivestreams.client

I have used MongoDb for few years successfully by using Spring Data (typical CRUD repositories). Now it is my first timme using MongDb Reactive Stream and, on top of this, my first time not using Spring. I am trying a very simple query and I am completely stuck after two days studing.

Here is the full code:

package com.example;

import com.mongodb.reactivestreams.client.*;
import io.micronaut.runtime.Micronaut;
import org.bson.Document;
import static com.mongodb.client.model.Filters.eq;

public class Application {

public static void main(String[] args) throws Throwable {, args);

    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("mybank");
    MongoCollection<Document> collection = database.getCollection("account_collection");
    FindPublisher<Document> findPublisher = collection.find(eq("key", "1"));



and all dependencies in build.gradle

plugins {
id “com.github.johnrengelman.shadow” version “6.1.0”
id “io.micronaut.application” version ‘1.0.5’

version “0.1”
group “com.example”

repositories {

micronaut {
runtime “netty”
testRuntime “junit5”
processing {
incremental true
annotations “com.example.*”

dependencies {

mainClassName = “com.example.Application”
java {
sourceCompatibility = JavaVersion.toVersion(‘11’)
targetCompatibility = JavaVersion.toVersion(‘11’)

Well I have tried several approaches after studied carefully an example named Tour provided in official repository. I believe the above code was the closest tentative from myself.

When I debug the code above in IntelliJ, it get blocked in “System.out.print(findPublisher.first());” forever. Well, I was expecting to block or wait only until it resolves.

When I read document about FindPublisher<>.find it says “Helper to return a publisher limited to the first result”. If I understood correctly, it brings the fist document which matches the criteria from “collection.find(eq(“key”, “1”))”

So my main question is: what I am doing wrong or missing in order to retrieve/select/query the document?

An additional information, I tried another approach based on Tour example. I copied SubscriberHelpers file to my project and I tried

    Document doc = new Document("name", "MongoDB")
            .append("type", "database")
            .append("count", 1)
            .append("info", new Document("x", 203).append("y", 102));

    collection.insertOne(doc).subscribe(new SubscriberHelpers.OperationSubscriber<>());

    // get it (since it's the only one in there since we dropped the rest earlier on)
    collection.find().first().subscribe(new SubscriberHelpers.PrintDocumentSubscriber());


    // Clean up
    SubscriberHelpers.ObservableSubscriber subscriber = new SubscriberHelpers.ObservableSubscriber<>();
    subscriber = new SubscriberHelpers.PrintSubscriber("Collection Dropped");

    // release resources

I noticed that the Enum Success isn’t available so I just removed assuming the example isn’t up-to-date with newer version.

And here it seems the document was added since I see an automatic _id created but I check in database and it isn’t there at all. Also, subscriber.await() lock the application foreever