[CSHARP-1985] Additional overloads for BitsAllSet and other bitwise query operators Created: 16/May/17  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Operations
Affects Version/s: 2.4.3
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: Jacob Brogan Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

SERVER-3518 introduced bitwise query operators with 3 different parameter types: a numeric bitmask (32-bit integer*), a BinData bitmask, or a position list (an array of 32-bit integers).

However, the FilterDefinitionBuilder in the C# driver [only supports one overload for BitsAllSet](https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/FilterDefinitionBuilder.cs#L321) (and other binary operators): a 64-bit long. This becomes an issue if you want to do a bitwise query on more than 64 bits, as there's no overload to allow you to pass in that amount of data. I've been able to work around this for now by simply building a BsonDocument containing my query, which simply converts an IEnumerable<int> to a BsonArray field value.

IEnumerable<int> bitPositions = new [] { 148, 149, 150, 151, 152 };
var queryDocument = new BsonDocument
{
    { "binaryDataField", new BsonDocument {
        { "$bitsAllSet", new BsonArray(bitPositions) }
    }}
};

While this works, it's a pain to get it to work with other FilterDefinitions, and it requires that you pull the BsonElementName off of your model class, which isn't really a great solution for maintainability. I would like to see support for the BinData (as byte[]) and position list (as IEnumerable<int>) overloads added to the BitsAllSet, BitsAllClear, BitsAnySet, and BitsAnyClear methods of FilterDefinitionBuilder.

  • Through testing, I've found that the actual maximum for the numeric bitmask is 0x7FFFFFFFFFFFFDFF through mongo shell, and long.MaxValue through the C# driver.


 Comments   
Comment by Robert Stam [ 24/May/17 ]

Adding overloads for arrays of bit positions and BinData arguments is a great idea. We will do that in an upcoming release.

It's true that when constructing the filter manually you have to know the element name.

I don't understand the problem with combining regularly built filters with manually built filters. The following should work:

var builder = Builders<C>.Filters;
BsonDocument queryDocument = ...; // as in the description above
var other = builder.Xyz(...); //; some other filter
var combined = builder.And(queryDocument, other); 

Generated at Wed Feb 07 21:41:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.