How to query a Mutable Document?

I have saved a JSON Object as Mutable Document. The object looks as follows
{
“id”:“title”,
“options”:[
{
“desc”:“Mr”,
“code”:“01”,
“parent”:“M”
},
{
“desc”:“Miss”,
“code”:“02”,
“parent”:“F”
},
{
“desc”:“Mrs”,
“code”:“03”,
“parent”:“F”
}
]
}

I want to select objects where parent value is equal to ‘F’.
Is there any way to query the above document ? Or QueryBuilder only works to fetch records from collection [Couchbase Lite database] ?

See docs for examples on how you can implement the above query . Also, to level set terminology, as discussed in this post Couchbase Lite does not support collections so to avoid confusion, please refrain from referring to Couchbase Lite instance as a collection.

1 Like

Look at the ANY and EVERY query expressions in the query documentation. Those let you look inside arrays.

If you’re storing a lot of stuff inside an array, however, that’s often a sign that you should break the array up into separate documents, each of which has a property that refers back to the parent document.

1 Like

Couldn’t find ANY and EVERY query expressions in DOCS, can you please share the link if possible?

Thanks in advance :slight_smile:

Thanks for response, Actually the example covered here is querying from database and will result all documents that matches the expression. In my case i want to search from a document i have fetched the document by its id and type now that document contains an array “options” and i want to query on that options array.

The reason i am searching from specific document is that the value can possibly exists in multiple documents. Lets say Gender=F this value can be a part of multiple documents of same type and of different type as well i.e (Customer,Account detail,Martial Status) etc, if i search from database using the above query it will return all documents. My purpose its to get title whose parent value is equal to ‘F’.

Tried to use ANY and EVERY query expressions as suggested by @jens but couldn’t find it in DOCS.

Those docs are pretty high-level and don’t cover every single query operation. Check the full Java API docs. (I don’t know the Java API, I work at a lower level on the core engine, so I can’t tell you exactly what the API is.)

1 Like

Found the documentation on this topic here.

Used QueryBuilder to fetch an array of specific document based on some condition but was unable to find the complete JSON Object from an array as per the answer here it will not be supported to do so using Query.

Below is the Query i used to find options array from a specific document where Gender=‘F’.

   >  VariableExpression VAREXP= ArrayExpression.variable("OPTIONS");
> Query searchQuery = QueryBuilder.select(SelectResult.property(".options"))
>                 .from(DataSource.database(couchDB))
>                 .where(Expression.property("type").equalTo(Expression.string("SELECTDATA")).and(Meta.id.is(Expression.string("SELECTDATA_MYTABLENAME")))
>                         .and(ArrayExpression.any(VAREXP).in(Expression.property("options"))
>                                 .satisfies(ArrayExpression.variable("OPTIONS.parent").equalTo(Expression.string("F")))));

Modified above query and tried to find not complete JSON Object but a only description String inside options array but it returned null elements.

>   VariableExpression VAREXP= ArrayExpression.variable("OPTIONS");
>       Query searchQuery = QueryBuilder.select(SelectResult.property(".options.desc"))
>                       .from(DataSource.database(couchDB))
>                       .where(Expression.property("type").equalTo(Expression.string("SELECTDATA")).and(Meta.id.is(Expression.string("SELECTDATA_MYTABLENAME")))
>                               .and(ArrayExpression.any(VAREXP).in(Expression.property("options"))
>                                       .satisfies(ArrayExpression.variable("OPTIONS.parent").equalTo(Expression.string("F")))));

Is it not possible to find not complete object but a single value from array that is in document?

It isn’t. We don’t support the N1QL query operations that do that.

The workaround is to get the entire array from the query, and look through it yourself.

That query looks strange: it’s deliberately just accessing a single document with ID SELECTDATA_MYTABLENAME. Is this just for experimenting?

1 Like

Okay Got your point.
Yes i am just testing to retrieve multiple documents and single document as well as in some cases we have document id and we need to get data from that document so that is why query looks like this.