How to query nested object in couchbase lite in android

My couchbase document looks like this:

“Apple”: {
“color” : “red”,
“weight”: “10”
“color”: “orange”,
“weight”: “20”
“Banana”: {
“color”: “yellow”,
“weight”: “20”

By using mutable document, i have saved the documents in database with above format.

I have tried like this:

        Query query = QueryBuilder.

           query.addChangeListener(new QueryChangeListener() {
              public void changed(QueryChange change) {
                  ResultSet resultRows = change.getResults();

I need to query and get the data from database with the condition, if weight is equal to 20 in the above nested object and i have to get the Banana and orange object as a result. Could you please help me out.

Ummm… that’s just normal JSON lookup. To find the weight, for instance: Document.getDict(“Banana”).getInt(“weight”) > 20.

By using mutabledoc i saved the document. I just need to retrieve the nested json and use QueryListener and check for new updates. please check my edited post that i tried so far

It looks like you have one document with all your data in it. That’s not a database. In a database each separate item has its own document. So you’d have a document named “Apple” with properties “color” and “weight”, etc. (And it’s better to make “weight” a number, not a string. Otherwise comparisons won’t work right.)

Once you do that you can query the documents using the query you already wrote. The reason the query doesn’t work now is that there is no property named “weight” in the document — instead you’ve got nested properties like “Apple.weight”, “Orange.weight” etc.

1 Like

@jens Thank you very much for clear insight. Now I could understand much better. :smiley: You saved my day