What is the correct way to join on keys in Couchbase Lite v2 Objective C API

I have the following documents:
{
"_id": “ecc:102”,
“type”: “category”,
“items”: [
“eci:742”,
“eci:743”,
“eci:744”,
],
“name”: “Skills”
},
{
"_id": “eci:742”,
“type”: “item”,
“chinese”: “技术”,
“english”: “technique”,
“pinyin”: “jìshù”
},
{
"_id": “eci:743”,
“type”: “item”,
“chinese”: “技巧”,
“english”: “skill”,
“pinyin”: “jìqiǎo”
},
{
"_id": “eci:744”,
“type”: “item”,
“chinese”: “技能”,
“english”: “ability”,
“pinyin”: “jìnéng”
}

I want to run the following query using the CBL 2.0 Objective C library:
SELECT item.*
FROM rememberit category
JOIN rememberit item ON KEYS category.items
WHERE META(category).id = ‘ecc:102’

I’ve run it in the query editor and it works fine but I can’t see how to implement the ON KEYS using the API.

Here’s what I’ve got so far:

CBLQueryExpression *on = [[CBLQueryMeta idFrom:@"item"] in:[CBLQueryExpression property:@"items" from:@"category"]];

CBLQueryJoin *join = [CBLQueryJoin join:[CBLQueryDataSource database:database as:@"item"]
                     on: on];

CBLQuery* query = [CBLQuery select:@[[CBLQuerySelectResult allFrom:@"item" ]]
                              from:[CBLQueryDataSource database:database as:@"category"]
                              join: join
                             where:[[CBLQueryMeta idFrom:@"category"] equalTo:categoryId]];

This gives me an error on trying to create the on expression:
[CBLPropertyExpression countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7f8d98d4bd80

I’ve also tried:
CBLQueryExpression *on = [CBLQueryArrayFunction contains:[CBLQueryExpression property:@"items" from:@"category"] value:[CBLQueryMeta idFrom:@"item"]];

But that didn’t work either.

The query should be something like this (It’s with the latest build from source) but there seems to be a glitch and this does not quite work as expected . We will investigate and will keep you posted.

    CBLQuerySelectResult* ITEM_DOC_ID =
    [CBLQuerySelectResult expression: [CBLQueryMeta idFrom: @"itemDS"] as:@"ITEMID"];

    
    CBLQuerySelectResult* CATEGORY_DOC_ID =
    [CBLQuerySelectResult expression: [CBLQueryMeta idFrom: @"categoryDS"] as:@"CATEGORYID"];
    
    CBLQueryExpression* ITEMID  = [CBLQueryExpression property:@"id" from: @"itemDS"];
    
    CBLQueryExpression* CATEGORYITEMS  = [CBLQueryExpression property: @"items" from:@"categoryDS"] ;
    CBLQueryVariableExpression* CATEGORYITEMVAR = [CBLQueryArrayExpression variableWithName: @"item"];
        
    CBLQueryExpression* on = [CBLQueryArrayExpression
                                any: CATEGORYITEMVAR
                                in: CATEGORYITEMS
                                satisfies: [CATEGORYITEMVAR equalTo: ITEMID]];
    
    
    CBLQueryJoin* join = [CBLQueryJoin join: [CBLQueryDataSource database: self.db as: @"itemDS"]
                                         on: on];
    CBLQuery* q = [CBLQuery select: @[ITEM_DOC_ID, CATEGORY_DOC_ID]
                              from: [CBLQueryDataSource database: self.db as: @"categoryDS"]
                              join: @[join]];
    

}

Hi Priya,
Thanks for your response. I’m using 2.0 DB21 but I can’t see any reference to CBLQueryVariableExpression anywhere.

Is there an updated version somewhere?

2.0 DB21 is latest official pre-release . What I shared references the latest source built from couchbase-lite-ios GitHub repo. If you are using Carthage, you can use the following in your Cartfile to build the same

github "couchbase/couchbase-lite-ios" "feature/2.0"

DB-21 should have equivalent functionality. You’d have done something like this to declare variables. In latest, we have added better type safety features

  CBLQueryExpression* CATEGORYITEMVAR = [CBLQueryArrayExpression variableNamed: @"item"];

But you can just move to latest build (which should hopefully go out officially soon)

Like I said, even the query that I shared above (which should seemingly work) is not quite working so will have to investigate it . But in meantime, you can use that as a reference of how you would handle array collections and such.

Ok, I look forward to the resolution as I’m kind of dependent on the join working properly :wink:

As it turns out, there is a bug which should get resolved in the next release or so. Once that’s fixed, the query that I shared earlier should work.

Good to hear. Any idea regarding when that will happen?

Hopefully in a week or so …

1 Like

btw, what happened to the Objective C documentation? It’s not on the site anymore:
https://developer.couchbase.com/documentation/mobile/2.0/couchbase-lite/index.html

It will be included as soon as the sample code snippets are updated for ObjC…As you may have noticed, we have been making some changes to the layout / presentation of the couchbase lite API. Let us know what you think.