[SERVER-55386] Support hex conversion and binary operators to aggregation framework Created: 21/Mar/21  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Query Language
Affects Version/s: 4.4.4
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Wernfried Domscheit Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-8716 Various update() operators for Binary... Backlog
related to SERVER-35013 Support base conversion in $convert Backlog
Assigned Teams:
Query Execution
Participants:
Case:

 Description   

Are there any roadmaps to support number to hex conversion and binary operators? I can imagine several use cases where they would be really helpful. Typically and most required operations would be:

  • convert Int/Long to Hex
  • convert Hex to Int/Long
  • Create BinData from Hex string (already available in mongo shell `HexData(0,"...")` but not in aggregation framework)
  • Get Hex string of BinData (already available in mongo shell `b.hex()` but not in aggregation framework)
  • Binary AND
  • Binary OR
  • Binary XOR
  • Binary NOT (i.e. complement)

Here are some examples:

 

{ $toHex: 44907 }    => "AF6B"
{ $fromHex: "AF6B" } => 44907
 
{ $binFromHex: "AF6B" }            => BinData(0,"r2s=")
{ $hexFromBin: BinData(0,"r2s=") } => "AF6B"
 
{ $bitAnd: [BinData(0,"r2s="), BinData(0,"/wA=")] } => BinData(0,"rwA=")
{ $bitOr:  [BinData(0,"r2s="), BinData(0,"/wA=")] } => BinData(0,"/2s=")
{ $bitXOr: [BinData(0,"r2s="), BinData(0,"/wA=")] } => BinData(0,"UGs=")
{ $bitComplement: BinData(0,"r2s=") }               => BinData(0,"UJQ=")
 
{ $bitAnd: [{$binFromHex: "AF6B"}, {$binFromHex: "FF00"}] } 
=> BinData(0,"rwA=") // HexData(0,"AF00")
 
{ $bitOr: [{$binFromHex: "AF6B"}, {$binFromHex: "FF00"}] }  
=> BinData(0,"/2s=") // HexData(0,"FF6B")
 
{ $bitXOr: [{$binFromHex: "AF6B"}, {$binFromHex: "FF00"}] } 
=> BinData(0,"UGs=") // HexData(0,"506B")
 
{ $bitComplement: {$binFromHex: "AF6B"} } 
=> BinData(0,"UJQ=") // HexData(0,"5094") 

 

 

 

 

 

 



 Comments   
Comment by owais ahmad [ 15/May/21 ]

Being new to mongoDB I thought i was doing something wrong, I was surprised string conversion was included.  In my application, the values are stored in hexadecimal values encoded as strings starting with 0x.  e.g. "0xAEF" (notation in C-language). 

Comment by Wernfried Domscheit [ 24/Mar/21 ]

Hi kyle.suarez

I was also considering this but then I skipped it. Converting from Hex to Octal or Binary and vice versa is very simple, so I did not add it to the request.

But yes, it could be useful. Base numbers other than 2,8,10,16 should be very exotic I assume.

 

Best Regards
Wernfried

 

 

Comment by Kyle Suarez [ 24/Mar/21 ]

Hello wernfried.domscheit@sunrise.net,

Thanks for filing this feature request. I'm adding this to the Query Team's backlog, where we will schedule this along with other project work in a future planning meeting. I've also linked SERVER-35013, where we suggest adding integer base conversion support to $convert, which seems like a good feature that should be considered alongside the logical bindata operators suggested here.

Best,
Kyle

Comment by Wernfried Domscheit [ 22/Mar/21 ]

Yes, i know. However, there is nothing which exist in the Aggregation Framework. That's what I am asking for.

Comment by Eric Sedor [ 22/Mar/21 ]

Hi wernfried.domscheit@sunrise.net, $bit provides some of this functionality for integer fields. I will pass this ticket on to a team that can comment in more detail.

Comment by Wernfried Domscheit [ 21/Mar/21 ]

AND, OR, XOR, NOT might be also useful for integer values, not only BinData. However with `$fromHex` and `$toHex` it would be very simple to build them.

Example:

{ $bitAnd: [ {$binFromHex: {$toHex: 255}}, {$binFromHex: {$toHex: 100}} ]}

Generated at Thu Feb 08 05:36:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.