Here is the code. Have currently updated to 8.0.5 still no change.
The objective is to convert an int field in the document and add it as a new hex representation ‘0x…’ field in the aggregation pipeline.
Here is the original implementation I tried:
{
"$addFields": {
"hex_device_id": {
"$convert": {
"input": "$int_id",
"to": "string",
"format": "hex",
"onError": "Invalid hex.",
"onNull": "Missing value.",
}
}
}
}
This populates the added field with just the string value of the int_id field. I.e. 256 → ‘256’
The more elaborate code shared above in the image:
{
"$addFields": {
"hex_id": {
"$convert": {
"input": {
"$convert": {
"input": {"$toInt": "$int_id"},
"to": "binData",
"onError": "Invalid binary.",
"onNull": "Missing value.",
}
},
"to": "string",
"format": "hex",
"onError": "Invalid hex.",
"onNull": "Missing value.",
}
}
}
}
The output just yields ‘Invalid binary’ for all documents for the added field ‘hex_id’ derived of course from the field ‘int_id’ which is an integer. I have just added the $toInt conversion for good measure.
From the documentation:
This reads as though a person can convert from anything to binary and from binary to anything so that is what I have done in the 2nd code segment. Int → binary → string but it fails converting int to binary and ultimately just passes the onError through to the string conversion which works naturally as it is then just the error string.
From the release notes:
It talks about binData to string conversions but this doesn’t help as the int to binData conversion is failing even using the mongoDB operator ‘$toInt’ as a fail-safe.
As mentioned above the first code converts the int to a string but it is just the string representation of the integer itself un-formatted as hex or otherwise, but serves to verify that the the ‘int_id’ field is in fact stored as an integer.