2

I have a few entries in a mongodb database. They have binary _ids. If I query with a mongo client like robomongo I'm able to find the entry I'm looking for:

db.getCollection('comment').find({"_id" : new BinData(0,"nCgNlWhzJM9/lHDVQmXQrg==")})

However, I'm having serious issues with doing the same in java. Here is what I'm trying to do:

final MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("comment");

mongoCollection
             .find(eq("_id", new Binary((byte) 0, "nCgNlWhzJM9/lHDVQmXQrg==".getBytes(StandardCharsets.UTF_8))))
             .first()

Sadly this is not working. Trying to find an item with a string type works (like eq("author", "someone) ).

1 Answer 1

3

Your _id value nCgNlWhzJM9/lHDVQmXQrg== is shown Base64 encoded, so your java query should be written:

    Document doc = mongoCollection
    .find(eq("_id", new Binary((byte) 0, Base64.getDecoder().decode("nCgNlWhzJM9/lHDVQmXQrg=="))))
    .first();

To decode the key before using it to create the binary filter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.