Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying Binary Field In MongoDB Compass

Tags:

mongodb

I am trying to query a binary field in mongo db. The data looks like this:

{"_id":"WE8fSixi8EuWnUiThhZdlw=="}

enter image description here

I've tried a lot of things for example:

{ '_id': new Binary( 'WE8fSixi8EuWnUiThhZdlw==', Binary.SUBTYPE_DEFAULT) }
{ '_id': Binary( 'WE8fSixi8EuWnUiThhZdlw==', 0) }

etc

Nothing seems to be working, have exhausted google and the mongo documentation, any helper would be amazing.

like image 948
Trent Stewart Avatar asked Jan 20 '19 22:01

Trent Stewart


1 Answers

UPDATE:

Now you should be able to query UUID and BinData from MongoDB Compass v1.20+ (COMPASS-1083). For example: {"field": BinData(0, "valid_base64")}.

PREVIOUS:

I see that you're using MongoDB Compass to query the field. Unfortunately, the current version of MongoDB Compass (v1.16.x) does not support querying binary data.

You can utilise mongo shell to query the data instead. For example:

db.collection.find({'_id':BinData(0, "WE8fSixi8EuWnUiThhZdlw==")});

Please note that the field name _id is reserved for use as a primary key; its value must be unique in the collection, and is immutable. Depending on the value of the binary that you're storing into _id, I would suggest to store the binary in another field and keep the value of _id to contain ObjectId.

like image 197
Wan Bachtiar Avatar answered Sep 28 '22 10:09

Wan Bachtiar