Work with . and & in field names (Java)

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:

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.collections.commands.options.CollectionFindOneAndReplaceOptions;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.core.query.Sort;
import java.util.Map;
import java.util.Optional;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find a document
    Filter filter =
        Filters.and(Filters.eq("areas.r&&d", false), Filters.lt("costs.price&.usd", 300));
    CollectionFindOneAndReplaceOptions options =
        new CollectionFindOneAndReplaceOptions()
            .sort(Sort.ascending("areas.r&&d"), Sort.descending("costs.price&.usd"))
            .projection(Projection.include("areas.r&&d", "costs.price&.usd"));
    Document newDocument =
        new Document()
            .append(
                "areas",
                Map.of(
                    "r&d", false,
                    "design", true))
            .append(
                "costs",
                Map.of(
                    "price.usd", 100,
                    "price.cad", 90));
    Optional<Document> result = collection.findOneAndReplace(filter, newDocument, options);
    System.out.println(result);
  }
}

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

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.query.Projection;
import java.util.Map;
import java.util.Optional;
import com.datastax.astra.internal.utils.EscapeUtils;
import com.datastax.astra.client.collections.commands.options.CollectionFindOneAndReplaceOptions;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find a document
    Filter filter =
        Filters.and(Filters.eq(EscapeUtils.escapeFieldNames("areas", "r&d"), false), Filters.lt(EscapeUtils.escapeFieldNames("costs", "price.usd"), 300));
    CollectionFindOneAndReplaceOptions options =
        new CollectionFindOneAndReplaceOptions()
            .sort(Sort.ascending(EscapeUtils.escapeFieldNames("areas", "r&d")), Sort.descending(EscapeUtils.escapeFieldNames("costs", "price.usd")))
            .projection(Projection.include(EscapeUtils.escapeFieldNames("areas", "r&d"), EscapeUtils.escapeFieldNames("costs", "price.usd")));
    Document newDocument =
        new Document()
            .append(
                "areas",
                Map.of(
                    "r&d", false,
                    "design", true))
            .append(
                "costs",
                Map.of(
                    "price.usd", 100,
                    "price.cad", 90));
    Optional<Document> result = collection.findOneAndReplace(filter, newDocument, options);
    System.out.println(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