I'm using Java Driver for MongoDB (2.14) in my application.
I have these documents:
{ "_id" : ObjectId("56fb9798e2445ade35effa89"), "b" : BinData(3,"abcdefgh") }
{ "_id" : ObjectId("56fba265e2445ade35effa8c"), "b" : 1 }
I have to retrieve all documents where "b" is a binary data using Java.
To reach my target I use the following query:
DBObject query = new BasicDBObject(b, new BasicDBObject("$type",5));
DBObject projKeys = new BasicDBObject();
projKeys.put("_id", 0);
projKeys.put(b, 1);
DBCursor cursor = coll.find(query,projKeys);
But when I start to iterate over cursor I get an exception:
java.lang.IllegalArgumentException: bad data size subtype 3 len: 6 != 16
When I try to make the same query using mongo shell, that is:
db.coll.find({b:{"$type":5}}, {_id:0,b:1})
I don't have errors.