Android ArrayExpression query result

query

#1

Hallo I am writing an android app with Couchbase lite (my first contact with a no sql database). I will reduce my result with this query:

VariableExpression VAR_PACKAGES_MEDIA_SIZE = ArrayExpression.variable(“package.Media.Size”);
VariableExpression VAR_PACKAGES = ArrayExpression.variable(“package”);

   Query query =  QueryBuilder.select(SelectResult.expression(Meta.id),
                SelectResult.expression(Expression.string(".Packages")).as("packages"))
                .from(DataSource.database(database))
                .where(ArrayExpression.any(VAR_PACKAGES).in(Expression.property("Packages"))                             .satisfies(VAR_PACKAGES_MEDIA_SIZE.greaterThanOrEqualTo(Expression.intValue(35390))))
                .limit(Expression.intValue(100));

Here is my json

{
“APIVersion”: “0.1.4”,
"Packages": [
{
“Category”: “Arbeitssicherheit”,
“Description”: “Sie bringen Ihr …”,
"Media": {
“RetinaSize”: 57027,
“RetinaURL”: “package-2842/AS7-hochgelegen.jpg”,
"Size": 57027
},
“PackageID”: 2842,
“owner”: “demo”
},
{
“Category”: “Arbeitssicherheit”,
“Description”: “Sie bringen …”,
“Media”: {
“RetinaSize”: 15000,
“RetinaURL”: “package-18241/AS5-Büroarbeitsplätze.jpg”,
“Size”: 14000
},
“PackageID”: 18241,
“owner”: “demo”
},…
]

My problem is I get always all the “Packages” entries, but I am getting all entries where"Media.Size" is greater or equals to 35390.

Is this not possible with Couchbase lite or is something wrong with my query.

Please where is the mistake

Thanks


#2

You need to use VAR_PACKAGE, the loop variable, in the satisfies expression.


#3

Hello Jens,

thanks for your fast answer.

Sorry I have copied the wrong query into my question.

This is my my actual query:

VariableExpression VAR_PACKAGE_EXPRESSION  = ArrayExpression.variable("packages.Media.Size");
        VariableExpression VAR_PACKAGES = ArrayExpression.variable("packages");
        return QueryBuilder.select(SelectResult.expression(Meta.id),
                SelectResult.expression(Expression.property(".Object.Packages")).as("packages"))

                .from(DataSource.database(database))
                .where(Expression.property("type").equalTo(Expression.string(DIRECTORY_COUCH_DB_TYPE)).
                        and(Expression.property("owner").equalTo(Expression.string("demo"))).
                        and(ArrayExpression.any(VAR_PACKAGE_EXPRESSION).
in(Expression.property("Object.Packages"))
                              .satisfies(VAR_PACKAGE_EXPRESSION.
greaterThanOrEqualTo(Expression.intValue(30000)))));

Based on this blog: https://blog.couchbase.com/querying-array-collections-couchbase-mobile/

The result is all entries from array “Packages” was returned (in this case 50 entries), but I need only the entries with Media.Size greater or equals 30000 ( in this case these are only 18 entries).

Thanks for your help


#4

Your select clause specifies .Object.Packages, so you get that property value, which is the entire array. We don’t have a way currently to return only parts of an array. You can pick out those elements in your application code.
Privacy Policyhttps://www.couchbase.com/privacy-policy | Update Marketing Preferenceshttps://info.couchbase.com/unsubscribe-or-manage-preferences


#5

Thanks for your answer Jens