1

I'm trying to check if a document already exits, but I'm having some problems. I tried different solutions:

  1. Using findOne() and checking if output is null (doesn’t work)
  2. Using countDocument() (doesn’t work) The error that is saw the most is that I can’t cast for example: Publisher to long or Publisher to Document

Thanks.

Method 1:

Document d = collection.find(eq("UUID", id)).first();
if (d == null) {
    System.out.println("document = null");
    return;
}
System.out.println("document exists");

Method 2:

if (collection.countDocuments(query) < 1) {
    System.out.println("Document exists");
}
Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34
Jackolix
  • 23
  • 5

1 Answers1

1

To check if a document exists you can use the exists() method from the ReactiveMongoTemplate class. The method takes a Query object as its parameter, which is used to specify the conditions to match the document. You can read about the exists() method right here.

Here is an example of how you can use the exists() method to check if a document with a specific id field exists in a collection called docs:

Query query = new Query(Criteria.where("id").is("unique-id-123"));
Mono<Boolean> exists = reactiveMongoTemplate.exists(query, “docs”);

Where query is org.springframework.data.mongodb.core.query.Query and reactiveMongoTemplate is org.springframework.data.mongodb.core.ReactiveMongoTemplate


Another solutions that you mentioned actually have to work too, for example:

  1. Using findOne() method:
Query query = new Query(Criteria.where("id").is("unique-id-123"));
Mono<Document> documentMono = reactiveMongoTemplate.findOne(query, Document.class, "docs");
documentMono.subscribe(doc -> {
    if (doc != null) {
        System.out.println("Document exists!");
    } else {
        System.out.println("Document does not exist!");
    }
});

You can read about findOne() method here.

Update for [1]: findOne():

As kerbermeister mentioned, That approach will not work:

reactor does not allow null values, so if publisher completes with empty, there will be no null value

and

When Publisher completes with empty it instantly completes with onComplete signal, not onNext. Also, as documentation of subscribe(Consumer) suggests: consumer – the consumer to invoke on each value (onNext signal). So this callback will never be called if publisher completes with empty.

  1. Using count() method:
Query query = new Query(Criteria.where("id").is("unique-id-123"));
Mono<Long> count = reactiveMongoTemplate.count(query, "docs");
count.subscribe(cnt -> {
    if (cnt > 0) {
        System.out.println("Document exists!");
    } else {
        System.out.println("Document does not exist!");
    }
});

You can read about count() method here

Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34
  • Thanks for your detailed reply! Unfortunately, I get an error message within my IDE with 'Mono': Cannot access reactor.core.publisher.Mono – Jackolix Jan 24 '23 at 13:19
  • Fixed it. I didn't imported the reactor api, only the springframework. But how do I cast Mono to boolean? – Jackolix Jan 24 '23 at 13:59
  • 1
    @Jackolix you can use `subscribe()`, or `block()` methods. https://stackoverflow.com/questions/47179937/how-to-get-string-from-monostring-in-reactive-java – Volodya Lombrozo Jan 24 '23 at 14:03
  • @VolodyaLombrozo "another" solution 1 would not work, reactor does not allow null values, so if publisher completes with empty, there will be no null value – kerbermeister Feb 03 '23 at 14:24
  • @kerbermeister Thank you. Have you check that? If it is so, I'll update my answer – Volodya Lombrozo Feb 03 '23 at 15:11
  • @VolodyaLombrozo of course. When Publisher completes with empty it instantly completes with onComplete signal, not onNext. Also, as documentation of subscribe(Consumer) suggests: consumer – the consumer to invoke on each value (onNext signal). So this callback will never be called if publisher completes with empty. – kerbermeister Feb 03 '23 at 15:41