Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Shell: Query BinData by Type

I am trying to query a large collection by the _id field which uses BinData.

Some of these ids use BinData of type 4:

"_id" : BinData(4,"CNDF66qIlCY92q1vFAAAAQ==")

While some use BinData of type 3:

"_id" : BinData(3,"CNDF66qJ29g92q1vFAAAEw==")

I need to find all of the _id fields that have BinData.type = 3 and was wondering if anyone has had luck querying like this in the MongoDB shell.

Any help would be greatly appreciated!

like image 513
Rwprice Avatar asked Sep 03 '25 16:09

Rwprice


1 Answers

You can do this with a $where style query as the BinData object has a .subtype() method that returns that "type":

db.collection.find(function(){ return this._id.subtype() == 3 })

Note that the "type" 3 BinData is by default what would be generated by the new UUID helper available to the shell from MongoDB 2.6:

UUID("0123456789abcdeffedcba9876543210")
BinData(3,"ASNFZ4mrze/+3LqYdlQyEA==")
like image 170
Neil Lunn Avatar answered Sep 05 '25 08:09

Neil Lunn