Filter operators for collections (C#)

Many Data API commands, like Find documents (C#), accept a filter parameter to determine which documents to return or update. The filter parameter uses one or more filter operators.

You must use & to escape any . or & in field names in a filter. Dot notation, which is used to reference nested fields, should not be escaped. For more information, see Work with . and & in field names (C#).

Operators

You can use the following operators in a filter. All operators are case-sensitive.

Equal (default)

The $eq operator matches documents where the specified field is equal to the specified value.

You can’t use this operator on the $lexical field.

This is the default when you don’t specify an operator, except when you filter on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Eq("number_of_pages", 714);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Not equal

The $ne operator matches documents where the specified field is not equal to the specified value.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Ne("number_of_pages", 300);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Greater than

The $gt operator matches documents where the specified field is greater than the specified value.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Gt("number_of_pages", 300);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Greater than or equal

The $gte operator matches documents where the specified field is greater than or equal to the specified value.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Gte("number_of_pages", 300);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Less than

The $lt operator matches documents where the specified field is less than the specified value.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Lt("number_of_pages", 300);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Less than or equal

The $lte operator matches documents where the specified field is less than or equal to the specified value.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Lte("number_of_pages", 300);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

In

The $in operator matches documents where the field contains any of the specified values. For fields that store an array, the operator matches documents where the array contains any of the specified values.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.In("genres", ["Fantasy", "Romance"]);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

If you have only one value to match, you can use the single value instead of an array, like { "$in": "VALUE" }.

Not in

The $nin operator matches documents where the field doesn’t contain any of the specified values.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Nin("genres", ["Fantasy", "Romance"]);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Exists

The $exists operator matches documents that have the specified field, even if the field value is null.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Exists("borrower");

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

All

For fields that store an array, the $all operator matches documents where the array contains all of the specified values.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.All("genres", ["Fantasy", "Romance"]);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Size

For fields that store an array, the $size operator matches documents where the array has the specified number of elements.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Size("genres", 3);

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Not

The $not operator returns documents that don’t match the condition of the clause.

You can’t use this operator on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Not(
      filterBuilder.Eq("is_checked_out", false)
    );

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Match

Lexicographical matching is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms.

The $match operator finds documents whose $lexical field value is a lexicographical match to the specified string of space-separated keywords or terms.

You can only use the $match operator on the $lexical field. This is the only operator that the $lexical field supports.

This is the default operator when you don’t specify an operator and filter on the $lexical field.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filter = Builders<Document>.CollectionFilter.LexicalMatch(
      "tree hill"
    );

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Combine operators (And, Or)

The $and operator joins clauses with a logical AND. Only documents that match the conditions of all clauses are returned.

The $or operator joins clauses with a logical OR. Documents that match the condition of any of the clauses are returned.

You can use $and and $or separately or together. If you want to match multiple flexible conditions, use $and and $or together in the same filter:

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.And(
      filterBuilder.Or(
        filterBuilder.Eq("is_checked_out", false),
        filterBuilder.Lt("number_of_pages", 300)
      ),
      filterBuilder.Or(
        filterBuilder.In("genres", ["Fantasy", "Romance"]),
        filterBuilder.Gte("publication_year", 2002)
      )
    );

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

To filter on a range of values, use $and with $lt/$lte and $gt/$gte:

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.And(
      filterBuilder.Lt("number_of_pages", 300),
      filterBuilder.Gte("number_of_pages", 200)
    );

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Filter nested fields

Use dot notation to filter nested fields. For example, field.subfield.subsubfield.

The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.Eq("metadata.language", "French");

    var result = await collection.FindOneAsync(filter);

    Console.WriteLine(JsonSerializer.Serialize(result));
  }
}

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM