MongoDB.live, free & fully virtual. Register Now MongoDB.live, 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 com.sun.net.httpserver.Authenticator;
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 {


    Micronaut.run(Application.class, 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"));

    System.out.print(findPublisher.first());

}

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 {
mavenCentral()
jcenter()
}

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

dependencies {
implementation(“io.micronaut:micronaut-validation”)
implementation(“io.micronaut:micronaut-runtime”)
implementation(“javax.annotation:javax.annotation-api”)
implementation(“io.micronaut:micronaut-http-client”)
implementation(“io.micronaut.mongodb:micronaut-mongo-reactive”)
runtimeOnly(“ch.qos.logback:logback-classic”)
testImplementation(“de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.0.1”)
}

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

    System.out.print(collection.countDocuments());

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

    // release resources
    mongoClient.close();

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