Use QueryBuilder to query on a property that is inside a nested JSON

[Swift] Hi. I am new to Couchbase Lite so might be asking a really basic question. I have a requirement where I need to use the QueryBuilder to fetch the data at a property which is nested inside another property. Seems like a pretty straight forward nested query thing but I’m not able to find resources for the same. Consider the following example where I have multiple documents that follow the same document pattern:

{
  "user":{
    "id": 1,
    "metadata": {
      "anotherId": 2,
      "aRandomArray": [
        "Works",
        "WorksAsWell"
        ]
    }
  }
}

Now, how will the QueryBuilder’s query look like if I have to make the query for the following use-cases:

  1. Fetch a document where id is equal to 1
  2. Fetch a document anotherId is equal to 2
  3. Fetch a document where aRandomArray contains WorksAsWell

So the question primarily revolves around how to create a query for properties that are nested inside other properties using QueryBuilder?


1) QueryBuilder.select(/*<whatever>*/).from(/*<wherever>*/).where(
   Expression.property("user.id").equalTo(Expression.intValue(1)));
2) QueryBuilder.select(/*<whatever>*/).from(/*<wherever>*/).where(
   Expression.property("user.metadata.anotherId").equalTo(Expression.intValue(2)));
3) QueryBuilder.select(/*<whatever>*/).from(/*<wherever>*/).where(
   ArrayFunction.contains(
       Expression.property("user.metadata.aRandomArray"),
       Expression.string("WorksAsWell")));

@blake.meike All the required properties are inside the user key in the specified JSON which I cannot see inside the QueryBuilder instance that you’ve mentioned. Also, shouldn’t the Expression go inside the .where() instead of .select()

Feh. Good points. Fixing now.

Thanks @shubham.bakshi !