Check if Document exists

Hey, im trying to check if a document already exits. But im 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’

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

Im using the reactive streams driver.
Thanks

Hello @Jackolix ,

Welcome to The MongoDB Community Forums! :wave:

I notice you haven’t had a response to this topic yet - were you able to find a solution?
If not then can you confirm if documents exist in your collection and can you share an example document?

Also,

Can you try your query in shell and check if you are getting the expected results?

By doesn't work do you mean that it never returns the expected value or are you seeing some other error?

Can you please share the exact error that you are getting? If it’s a casting error, then please refer to this thread which I think is related.

Additionally, I would recommend you to go through below thread and resources which includes some examples on querying database using reactive streams.

Regards,
Tarun

Thanks for your reply.
Im using now springframework with the ReactiveMongoTemplate and it works.
The answer is here click

And for the casting this was a helpful comment click

The only question I have now how do I use the variable in another method when it becomes available?

My exist method

public static boolean exists(String id) {
        Query query = new Query(Criteria.where("UUID").is(id));
        ReactiveMongoTemplate template = new ReactiveMongoTemplate(Database.getConnection(), "minecraft");
        Mono<Boolean> exists = template.exists(query, "players");
        exists.subscribe(
                value -> {
                    Console.send(Database.PREFIX + value);
                    return value;
                },
                error -> Console.send(Database.ERROR + error)
        );

And my method where I need the variable

public static Perk getPerks(Player player) {
        Perk nullperk = new Perk("booster", "enterhaken", "rocket_jump", true, 6, 7);
        if (!exists(player.getUniqueId().toString())) //here I need the variable
            setPerk(player, nullperk);
        if (Items.perks.containsKey(player))
            return Items.perks.get(player);
        return nullperk;

My Solution.
Im using now a Callback that is executed when the value is available. Now, everything works async.

interface Callback {
        void onSuccess(boolean value);
    }

public static void exists(String id, final Callback callback) {
        Query query = new Query(Criteria.where("UUID").is(id));
        ReactiveMongoTemplate template = new ReactiveMongoTemplate(Database.getConnection(), "minecraft");
        Mono<Boolean> exists = template.exists(query, "players");
        exists.subscribe(
                value -> callback.onSuccess(value),
                error -> Console.send(Database.ERROR + error)
        );
    }
 exists(player.getUniqueId().toString(), new Callback() {
            @Override
            public void onSuccess(boolean value) {
                if (!value)
                    setPerk(player, nullperk);
            }
        });
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.