Work with . and & in field names (C#)

You must use & to escape any . or & in field names when the field is used in a filter, sort, projection, update, or indexing clause. Dot notation, which is used to reference nested fields, should not be escaped.

For example, in the following document, you would use escaping like this: areas.r&&d, costs.price&.usd, and costs.price&.cad.

{
  "areas": {
    "r&d": true,
    "design": false
  },
  "costs": {
    "price.usd": 100,
    "price.cad": 90
  }
}

You should not escape . or & in field names when you insert or replace a document.

For example:

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");

    // Replace a document
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.And(
      filterBuilder.Eq("areas.r&&d", false),
      filterBuilder.Lt("costs.price&.usd", 300)
    );

    var replacement = new Document()
    {
      {
        "areas",
        new Document { { "r&d", false }, { "design", true } }
      },
      {
        "costs",
        new Document { { "price.usd", 100 }, { "price.cad", 90 } }
      },
    };

    var replaceOptions =
      new CollectionFindOneAndReplaceOptions<Document>()
      {
        Projection = Builders<Document>
          .Projection.Include("areas.r&&d")
          .Include("costs.price&.usd"),
        Sort = Builders<Document>
          .CollectionSort.Ascending("areas.r&&d")
          .Descending("costs.price&.usd"),
      };

    var result = await collection.FindOneAndReplaceAsync(
      filter,
      replacement,
      replaceOptions
    );

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

You can also use the EscapeFieldNames function provided by the client:

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

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.Eq(
        FieldEscaping.EscapeFieldNames("areas", "r&d"),
        false
      ),
      filterBuilder.Lt(
        FieldEscaping.EscapeFieldNames("costs", "price.usd"),
        300
      )
    );

    var options = new CollectionFindOneAndReplaceOptions<Document>()
    {
      Sort = Builders<Document>
        .CollectionSort.Ascending(
          FieldEscaping.EscapeFieldNames("areas", "r&d")
        )
        .Descending(FieldEscaping.EscapeFieldNames("costs", "price.usd")),
      Projection = Builders<Document>
        .Projection.Include(
          FieldEscaping.EscapeFieldNames("areas", "r&d")
        )
        .Include(FieldEscaping.EscapeFieldNames("costs", "price.usd")),
    };

    var replacement = new Document()
    {
      {
        "areas",
        new Document { { "r&d", false }, { "design", true } }
      },
      {
        "costs",
        new Document { { "price.usd", 100 }, { "price.cad", 90 } }
      },
    };

    var result = await collection.FindOneAndReplaceAsync(
      filter,
      replacement,
      options
    );

    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