Converting a string type hex number to a decimal?

Is there an aggregation operator that can convert a string type hex to a decimal? We are working with blockchain data and have run into cases like this quite often.

Hi @Kuan_Huang,

Welcome to MongoDB community!

Have you tried to use a $convert or $toDouble operator in a project or addFields stage?

https://docs.mongodb.com/manual/reference/operator/aggregation/toDouble/#exp._S_toDouble

Best
Pavel

That works. Thank you!

case 2:

Actually there is a problem. It seems to work in below in case 1 but not in case 2:

case 1:

Hi @Kuan_Huang ,

As you can see the projection stage preview results in no value for “b” therefore its null later.

I would use $addFields and have represantation of a binary string via the extended json represtation :

https://docs.mongodb.com/manual/reference/mongodb-extended-json/#bson.Binary

Thanks
Pavel

Thank you. I will find sometime to explore it and get back to you.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.