Can I add multiple conditions to retrive data from Couchlite?

Can I add multiple conditions to retrive data from Couchlite?

I want to retrive data which is shown below example–
is this possible??

private Query getAllBroadcastMessageDocumentQuery1() {
com.couchbase.lite.View view = database.getView(VIEW_USER);
if (view.getMap() == null) {
Mapper mapper = new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
String type = (String) document.get(Constants.COUCHLITE_DOC_TYPE);
String user_id = (String) document.get(Constants.USER_ID);
String provider_id = (String) document.get(Constants.PROVIDER_ID);
if (DOC_TYPE_USER.equals(type) && (USER_ID).equals(user_id) && (PROVIDER_ID).equals(provider_id)) {
emitter.emit(document.get(Constants.TIMESTAMP), document);
}
}
};
view.setMap(mapper, COUCHLITE_MAP_VERSION);
}
Query query = view.createQuery();
return query;
}

Hi,

I removed my last post as we should be able to do this, but not the way I was! I have been looking and things like postfilter, keys, startkey, endkey all seem to filter the view/query, although I am struggling with postfilter on swift, constantly getting:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CBLQueryRow 0x7fd2a1d6fd90> valueForUndefinedKey:]: this class is not key value coding-compliant for the key hhx41VhQHtd0yU2_pVcXgj

Where the key is an id of a parent document I am trying filter the query by. There is no real help that I have been able to find for the past 3-4 hours.

If anyone knows of a good clear example of postfilter using swift, I would really appreciate a link or a reply.

Regards,

Jacko

Got it working,

database.viewNamed("subDocs").setMapBlock("1") {
    (doc, emit) in
        if (doc["type"] as! String == "subDoc") {
                emit([doc["created_at"]!, doc["parent_id"]!], doc)
        }
}

So this view returns an array as a key with two values, watching some of Jens videos, not very performant on returning the entire doc, so may change this before publishing, anyway, on iOS using the kind of cool CBLUITableSource, I can now let the app do all the work on updating tableview etc.

let query = database.viewNamed("subDocs").createQuery().asLiveQuery()
// Used \() in predicate to include id, caused the crash above, expects printf style to add quotes etc
query.postFilter = NSPredicate(format: "key[1] == %@", self.parentDocId) // id passed on segue, key1 also works
query.sortDescriptors = [NSSortDescriptor(key: "key[0]", ascending: false)] // order results by descending created_at, key0 also works
    do { // show query rows in full to prevent madness
        let result = try query.run() // Not required to run before using as CBLUITableSource
        while let row = result.nextRow() {
            print("\(row) : \(self.parentDocId)")
        }
    } catch {
        print("fail")
    }
self.dataSource.query = query

Anyway hope this helps somebody else,

Jacko

For CBL Android/Java, You could use List as a compound key.

Example:

        view.setMap(new Mapper() {
            @Override
            public void map(Map<String, Object> document, Emitter emitter) {
                if ("task".equals(document.get("type"))) {
                    List<Object> keys = new ArrayList<Object>();
                    keys.add(document.get("list_id"));
                    keys.add(document.get("created_at"));
                    emitter.emit(keys, document);
                }
            }
        }, "1");

Thanks…

Tirtharaj

Thanks—Jacko

—Tirtharaj