Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.1.6
    • Component/s: Querying
    • Labels:
    • Environment:
      All
    • Backwards Compatibility:
      Fully Compatible

      Description

      We've decided to implement a few bit-test query operators:

      $bitsAllSet - matches if the input bit positions are all 1
      $bitsAllClear - matches if the input bit positions are all 0
      $bitsAnySet - matches if any of the bit positions are 1
      $bitsAnyClear - matches if any of the bit positions are 0

      These operators can take an array of bit positions, a 64-bit number bitmask, or a BinData bitmask. For the number and BinData bitmasks, the bit positions to check are the positions of 1's in their binary representation.

      The operators will only match against int32, int64, doubles, and binary data. Other types will not match.

      Note that bit position 0 means the least significant bit.

      An example using the array of bit positions syntax:

      db.foo.insert( { a: 43 } ); // Binary form: 101011 (leading zeroes truncated)
      db.foo.find( { a: { $bitsAllSet: [0, 1, 3] } }, { _id: 0, a: 1 } );
      Result: { a: 43 }
      

      An example using the bitmask syntax:

      db.foo.insert( { a: 43 } ); // Binary form: 101011 (leading zeroes truncated)
      db.foo.find( { a: { $bitsAllSet: 9 } }, { _id: 0, a: 1 } ); // Binary form of the bitmask: 1001
      Result: { a: 43 }
      

        Issue Links

          Activity

          Hide
          nifan nifan added a comment -

          IMO the array of list values is a workaround in itself for these cases which we would like to get rid of.

          An array will not work with capped collections, since adding a flag will change the size of the document.
          In non-capped collections it may trigger a relocation or (with powerof2 now default) at the very least a rewrite of the document.

          Which should all performance significantly worse then an in-place update on an int or byte field.

          The index would probably be much more compact then indexing a list of things and querying by bitmask probably faster then doing a ["foo", "bar", "egg"] in some-list for each document.

          For my use cases I'm with Jeremy; I really like the new operators but they are not very helpful if they cannot use an index.

          Show
          nifan nifan added a comment - IMO the array of list values is a workaround in itself for these cases which we would like to get rid of. An array will not work with capped collections, since adding a flag will change the size of the document. In non-capped collections it may trigger a relocation or (with powerof2 now default) at the very least a rewrite of the document. Which should all performance significantly worse then an in-place update on an int or byte field. The index would probably be much more compact then indexing a list of things and querying by bitmask probably faster then doing a ["foo", "bar", "egg"] in some-list for each document. For my use cases I'm with Jeremy; I really like the new operators but they are not very helpful if they cannot use an index.
          Hide
          asya Asya Kamsky added a comment -

          Regarding index use by $bitwise query operators, please create a new ticket requesting this new feature. Keep in mind that since currently in MongoDB Indexes hold the values that are searched for it would not be a trivial task to implement it.

          nifan discussion of performance of different ways of implementing this probably fits better on mongodb-user google group rather than in this ticket.

          Show
          asya Asya Kamsky added a comment - Regarding index use by $bitwise query operators, please create a new ticket requesting this new feature. Keep in mind that since currently in MongoDB Indexes hold the values that are searched for it would not be a trivial task to implement it. nifan discussion of performance of different ways of implementing this probably fits better on mongodb-user google group rather than in this ticket.
          Hide
          xgen-internal-githook Githook User added a comment -

          Author:

          {u'username': u'coollog', u'name': u'Qingyang Chen', u'email': u'qingyang.chen@10gen.com'}

          Message: SERVER-3518 Tests for full code coverage in bit test query operators
          Branch: master
          https://github.com/mongodb/mongo/commit/d09c418c8b5fb5ccc7c2864cac8367d1f849bef1

          Show
          xgen-internal-githook Githook User added a comment - Author: {u'username': u'coollog', u'name': u'Qingyang Chen', u'email': u'qingyang.chen@10gen.com'} Message: SERVER-3518 Tests for full code coverage in bit test query operators Branch: master https://github.com/mongodb/mongo/commit/d09c418c8b5fb5ccc7c2864cac8367d1f849bef1
          Hide
          neekfenwick Nick Fenwick added a comment -

          Not sure if this is on-topic, but how would we filter for this using the Expression syntax in C# using the official driver?

          e.g.
          Expression<Func<MongoThing, bool>> filter = f => f.bitfield & 128;
          var result = database
          .GetCollection<T>(CollectionName)
          .Find(filter);

          According to Properties on the MongoDB.Driver reference on our project we're using 2.0.1.27. In order to get bitwise support in Mongo I had to uninstall 3.0.6 from our Ubuntu 14.04 server and set up a 3.1 repository and install the mongodb-org-unstable* group of packages. How can we get a C# driver that also supports these new features?

          Show
          neekfenwick Nick Fenwick added a comment - Not sure if this is on-topic, but how would we filter for this using the Expression syntax in C# using the official driver? e.g. Expression<Func<MongoThing, bool>> filter = f => f.bitfield & 128; var result = database .GetCollection<T>(CollectionName) .Find(filter); According to Properties on the MongoDB.Driver reference on our project we're using 2.0.1.27. In order to get bitwise support in Mongo I had to uninstall 3.0.6 from our Ubuntu 14.04 server and set up a 3.1 repository and install the mongodb-org-unstable* group of packages. How can we get a C# driver that also supports these new features?
          Hide
          craiggwilson Craig Wilson added a comment -

          Hi Nick,

          We have a google groups list that is a great place to ask these types of questions.

          To answer you here, the current driver doesn't have strontly-typed support for this yet. It will be coming, likely in version 2.2 (which will line up with server 3.2). Until then, you can always construct this query manually using a BsonDocument...

          var filter = new BsonDocument("a", new BsonDocument("$bitsAllSet", 9));
          await collection.Find(filter).ToListAsync();
          

          Craig

          Show
          craiggwilson Craig Wilson added a comment - Hi Nick, We have a google groups list that is a great place to ask these types of questions. To answer you here, the current driver doesn't have strontly-typed support for this yet. It will be coming, likely in version 2.2 (which will line up with server 3.2). Until then, you can always construct this query manually using a BsonDocument... var filter = new BsonDocument("a", new BsonDocument("$bitsAllSet", 9)); await collection.Find(filter).ToListAsync(); Craig

            Dates

            • Created:
              Updated:
              Resolved:
              Days since reply:
              1 day ago
              Date of 1st Reply:

              Agile