1

I have the following method :

MongoCollection<Document> collection;
...
Document query = new Document();
query.put("uuid", uuid);
query.put("gems", "$exists");
Document document = collection.find(query).first();
if(document == null) {
    System.out.println("Not found");
    document = new Document();
    document.put("uuid", uuid);
    document.put("gems", 0L);
    collection.insertOne(document);
}

Now, the collection is there, I checked in Mongo, but for some reason the document is never inserted. I run:

db.(collection).find()

And get no output.

The document is never found, I don't know if my query is correct, but the document is never inserted. This is done with a clean install of MongoDB running on localhost. MongoDB shows as connected and no errors are thrown. Am I missing something?

1 Answer 1

0

This code seems to work, it fixes the insert and finding method:

Document document = collection.find(Filters.eq("uuid", uuid)).first();
if (document == null) {
    document = new Document("uuid", uuid);
    document.put("gems", 0L);
    collection.insertOne(document);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.