Couchbase-lite view multiple keys

Hi Im trying to create and then query a view in couchbase-lite using a nativescript plugin. The following is part of the view

map: (doc, emitter) => {
if(doc._id.indexOf(“files.system.”) > -1) {
if (doc.accountId && doc.folder) {
emitter.emit([doc.accountId, doc.folder], doc);
}
}
}

Im then using the following to query this view { mapOnly: true, keys: [“2”,“4”] }

This is bringing back no results. If I change this to only use one key without using array [] e.g.
emitter.emit (doc.folder, doc); and query using { mapOnly: true, keys: [“2”] }; this works fine.

Does anyone know how I can create and query a view with multiple keys on couchbase-lite.

The keys in the view you’ve defined are arrays, so the keys property of the query needs to be an array of arrays.

But I think what you’re actually trying to do is find all the rows whose keys have a first item of 2 or 4; is that correct? (Also: are you sure you want quotes around 2 and 4? Are your accountIDs really numeric strings, not numbers?)

If so, you need to do a range query, for example with startKey:["2"], endKey:["2", {}]. The pair of curly braces is an empty object, which sorts after any other JSON value, so this means you want the entire range of key arrays that start with “2”.

Our view and query documentation covers array keys in detail.

Hi Jens

Thanks for your reply but this does not answer my question. The reason I have put the values [“2”,“4”] is because I want to return docs with exactly “2” and “4” as keys and thats why I have tried to use [“2”,“4”] in the query.

If we make the assumption that “2” and “4” are string could you please give me a sample for the query you would use to bring back all documents with these keys. I dont get what you mean by ‘The keys in the view you’ve defined are arrays, so the keys property of the query needs to be an array of arrays.’. I would really appreciate if you could give me the query example.

Flagging @nraboy. This may be an issue with Nativescript (which is a community project, so not officially supported).

I work with Jas, was just wondering if I could add something that might clarify.
Here’s something similar, in Swift, using version 1.4.1 of the Couchbase SDK :

View named “list.items_in_folder_multi_key”

let map : CBLMapBlock = {doc,emit in
            
            guard let docId = doc["_id"] as? String else { return }
            guard docId.hasPrefix("files.system.") else { return }
            
            let gid = CBUtils.groupIdFromDocumentId(docId)
            let aid = CBUtils.accountIdFromGroupdId(gid)
            
            guard let folder = doc["folder"] as? String else { return }
            
            emit([aid,folder], nil) // aid : Int, folder : String
        }

In interest of completeness, here’s my other view where I’ve got a single combined key:

View named “list.items_in_folder”

let map : CBLMapBlock = {doc,emit in
            
            guard let docId = doc["_id"] as? String else { return }
            guard docId.hasPrefix("files.system.") else { return }
            
            let gid = CBUtils.groupIdFromDocumentId(docId)
            let aid = CBUtils.accountIdFromGroupdId(gid)
            
            guard let folder = doc["folder"] as? String else { return }
            
            let key = "\(aid):\(folder)"
            emit(key, nil)
        }

I get results only for my single key view “list.items_in_folder”, getting nothing for the “list.items_in_folder_multi_key” view.

Here’s me doing the queries using a single key view, a multi key view and the all docs query with an id prefix query:

queryView(named:"list.items_in_folder",configure:{query in
    query.mapOnly = true
    query.keys = ["117:/data/2046 ebrd level 2/content"]
})

queryView(named:"list.items_in_folder_multi_key",configure:{query in
    query.mapOnly = true
    query.keys = [[117],["/data/2046 ebrd level 2/content"]]
})

queryTable(named:"files.system.117.1eHjrdArxIkaD6tzW45E",key:"/data/2046 ebrd level 2/content")

I get the following results:

Queried view 'list.items_in_folder', got 16 rows, took: 7.450544 ms
Queried view 'list.items_in_folder_multi_key', got 0 rows, took: 0.939741 ms
Queried table 'files.system.117.1eHjrdArxIkaD6tzW45E', got 16 rows, took: 22.891379 ms

The multi key query simply isn’t working.