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) ).
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.