Joshua Fox
Joshua Fox

Reputation: 19695

In Couchbase Java Query DSL, how do I filter for property-names that are not from the ASCII alphabet?

Couchbase queries should support any String for property-name in a filter ( where clause.)

But the query below returns no values for any of the fieldNames "7", "a", "#", "&", "", "?". It does work for values for fieldName a.

Note that I'm using the Java DSL API, not N1ql directly.

OffsetPath statement = select("*").from(i(bucket.name())).where(x(fieldName).eq(x("$t")));
JsonObject placeholderValues = JsonObject.create().put("t", fieldVal);
N1qlQuery q = N1qlQuery.parameterized(statement, placeholderValues);
N1qlQueryResult result = bucket.query(q);

But my bucket does have each of these JsonObjects, including those with unusual property names, as shown by an unfiltered query:

{"a":"a"}
{"#":"a"}
{"&":"a"}
{"":"a"}
{"?":"a"}

How do I escape property names or otherwise support these legal names in queries?

(This question relates to another one, but that is about values and this is about field names.)

Upvotes: 1

Views: 289

Answers (1)

vsr
vsr

Reputation: 7414

The field name is treated as an identifier. So, back-ticks are needed to escape them thus:

select("*").from(i(bucket.name())).where(x("`" + fieldName + "`").eq(x("$value"))

with parameterization of $value, of course

Upvotes: 1

Related Questions