[SERVER-2413] Native types for IP Address and MAC Address Created: 26/Jan/11  Updated: 25/Oct/21  Resolved: 25/Oct/21

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: None
Fix Version/s: features we're not sure of

Type: New Feature Priority: Minor - P4
Reporter: Daris A Nevil Assignee: Geert Bosch
Resolution: Won't Do Votes: 13
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Fedora 14 x86_64


Issue Links:
Related
related to SERVER-2242 We would love to have an 64 bits UNSI... Closed
Participants:

 Description   

I am porting a network-oriented application from PostgreSQL to MongoDB. The application makes heavy use of the 'inet' and 'mac' data types, especially when searching for IP addresses in a netblock.

One could argue that IP Addresses can simply be stored and searched as strings, which is true. However, it becomes increasingly difficult when you throw in netmasks and IPv6. For example, the IPv6 address "FFAB::1234" is really "FFAB:0000:0000:0000:0000:0000:0000:1234", and both forms are acceptable string representations.

I could not find any plans for including this feature in MongoDB, so I decided to tackle it myself. The resulting work can be found at:

https://github.com/redmeadowman/mongo

The change involves adding the new types to BSON, and adding new classes to the JavaScript shell. I've added two new types, IpAddr() and MacAddr().

IpAddr - Contains an IPv4 or IPv6 address along with a netmask
Examples to try in the shell:
var ip4 = IpAddr("192.168.1.1")
ip4.mask
ip4.version
ip4.mask = 16
ip4.mask
ip4 = IpAddr("192.168.1.1/24")
ip4.mask
ip4.network
ip4.broadcast
var ip6 = IpAddr("1234::abcd");
ip6.mask
ip6.version
ip6.mask = 64
ip6.mask
var ip6 = IpAddr("1234::abcd/32");
ip6.network
ip6.broadcast
ip6 = IpAddr("::192.168.1.1/16");
db.test.insert(

{ip: IpAddr("192.168.1.1")}

)
db.test.insert(

{ip: IpAddr("1234::")}

)
db.test.find()

MacAddr - Contains a 6-octet Media Access Control Address (Ethernet hardware address)
var mac = MacAddr("00:23:45:ab:cd:ef");
db.test.insert(

{mac: MacAddr("1:2:3:4:5:6")}

)
db.test.find()

The application I'm porting also makes heavy use of UUIDs, so I've modified the Shell UUID type so it is now a native type of JavaScript. This now works (no need to say 'new UUID(...'
var uuid = UUID("123456789abcdef12345678901234567");
db.test.insert(

{uuid: UUID("123456789abcdef12345678901234567")}

)

The branch listed above includes the code changes against the latest master (as of 24 Jan 2011), plus unit tests in both dbtests/ and jstests/.



 Comments   
Comment by Geert Bosch [ 25/Oct/21 ]

Adding new types to BSON has a very high distributed cost, as the entire eco-system will need to be upgraded to support the new types. So, we will not consider that for this narrow use case.

Comment by Chad Kreimendahl [ 16/May/17 ]

It's actually insanely fast to do in your code... Just wire it up as binary and search ranges. Would be neat to see in drivers.

Comment by meghraj choudhary [ 16/May/17 ]

+1

MondoDB is known for scalable and big database and performance.

so quering IP address sholud also be faster, in my app i also want to store list of IP Addresses in v4 and v6 including CIDR notations then for every client i need to check weather client's ip listed in blacklist or not.

Someone has already done work please check, merge and doc it.

Comment by Chad Kreimendahl [ 29/Jan/16 ]

I think mongo could add support for IP Address searching, ranges, etc, without having to change BSON to add it as a feature. Addresses themselves are just a set of bytes. They could use standard binary storage and do searching based on bit masking (which would actually index incredibly well). You could also, in theory do some CIDR type searches. Really, you're talking about wiring up something to interpret the string forms of an IP into binary, and then wiring up something to allow you to search those.

CIDR search: db.Collection.find({IPAddress;

{ $in: Network('192.168.0.0/16') }

});

Then when entering the data, you'd just need something to convert it to binary properly... but that could be done in drivers.

Comment by Guillaume Gelin [ 29/Jan/16 ]

I just discovered there's no BSON type to deal with IP adresses, bummer! I'd love to see that feature implemented.

Comment by Nagy [ 10/Feb/14 ]

Sure, but it quickly starts getting ugly.
Like when I'd like to store CIDR format addresses and want to check whether a given address (/32 or /128), or a subnet is in any of them.
Yes, it can be solved by math (of course, that's the same the DB does with supporting these kind of addresses), but it's so hackish.

Comment by Chad Kreimendahl [ 10/Feb/14 ]

Have you considered, at least in the short term, storing the data as a 64bit integer and writing your own serializer/deserializer to convert? I'm not sure which language you're writing in, but this seems like it would get you at least a portion of what you want. I wish the bsonspec supported unsigned integers so we could use a 32bit, but 64 works well enough. When you want to search for all IPs in a range in your app, you just calculate the start and finish integer equiv of the IP. Given that an ipv4 address is just a 32bit object, any data type that supports range queries (gt, lt, etc) would work just fine. You could even use hex strings if you were so inclined. Though, I would wager that an int would search faster because of its fixed scope [vs text, etc]

Comment by Nagy [ 10/Feb/14 ]

I haven't touched mongodb since the 1.4->1.6 upgrade (which runs in an isolated environment happily since then , but for a new project, I started to re-evaluate possible solutions and I sadly discovered that mongodb still doesn't have native IP address support.
This is a must everywhere where the task is not only to store and search a single IP address, but handle subnetting, or IPv6.

Comment by Daris A Nevil [ 05/Sep/12 ]

The lack of support for native IP and MAC addresses has prevented me from using MongoDB. Any possibility of the main branch picking up the code? I'd be willing to port these features from 1.8 to the latest branch.

Comment by Eliot Horowitz (Inactive) [ 02/Sep/11 ]

Trying not to add too many types to BSON, but might be worth it at some point.

Generated at Thu Feb 08 02:59:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.