I am unable to query on binary UUID?
db={
"models": [
{
"_id": {
"$binary": {
"base64": "ksIhKC1GSleQMLeuREhjsg==",
"subType": "04"
}
},
"modelID": {
"$binary": {
"base64": "Am6CZhDWSRGouvb6394Nig==",
"subType": "04"
}
}
}
]
}
When I specify $binary it does not work
db.models.aggregate([
{
$match: {
_id: {
$eq: $binary(
"92c22128-2d46-4a57-9030-b7ae444863b2"
)
},
isDeleted: false
}
}])
https://mongoplayground.net/p/tr6EHENSGuG
2
It is not supposed to work. $binary
is part of Extended JSON notation used to represent data in a human-friendly format.
You cannot use it as an input, the same way as you cannot use $oid, or $date – this is how a mongodb client shows specific data types to you.
The only scalars you can use in a query crafted manually are strings and numbers, and you need to use functions to convert them to actual data type. In your case it would be $toUUID (added in v8.0). Being an expression, the converter cannot be used with “find/$match” syntax directly, and should be applied via $expr query:
db.models.aggregate([
{
"$match": {
"$expr": {
"$eq": [
"$_id",
{
"$toUUID": "92c22128-2d46-4a57-9030-b7ae444863b2"
}
]
}
}
}
])