Core Server
  1. Core Server
  2. SERVER-2413

Native types for IP Address and MAC Address

    Details

    • Type: New Feature New Feature
    • Status: Open Open
    • Priority: Minor - P4 Minor - P4
    • Resolution: Unresolved
    • Affects Version/s: minor tech improvements
    • Component/s: JavaScript, Shell, Storage
    • Labels:
      None
    • Environment:
      Fedora 14 x86_64
    • Backport:
      No
    • # Replies:
      5
    • Last comment by Customer:
      true

      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/.

        Activity

        Hide
        Eliot Horowitz
        added a comment -

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

        Show
        Eliot Horowitz
        added a comment - Trying not to add too many types to BSON, but might be worth it at some point.
        Hide
        Daris A Nevil
        added a comment -

        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.

        Show
        Daris A Nevil
        added a comment - 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.
        Hide
        Nagy, Attila
        added a comment -

        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.

        Show
        Nagy, Attila
        added a comment - 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.
        Hide
        Chad
        added a comment -

        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]

        Show
        Chad
        added a comment - 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]
        Hide
        Nagy, Attila
        added a comment -

        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.

        Show
        Nagy, Attila
        added a comment - 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.

          People

          • Votes:
            8 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

            • Created:
              Updated:
              Days since reply:
              10 weeks, 2 days ago
              Date of 1st Reply: