Query multiple keys in couchbase lite view

↘锁芯ラ 提交于 2019-12-24 13:26:40

问题


Hi I am new to couchbase/couchbase-lite and i try to query a view with multiple keys without success. Her is how the map function looks:

public void map(Map<String, Object> doc, Emitter emitter) {
    if (doc.get("type").equals("my_type") {
        List<Object> keys = new ArrayList<Object>();
        keys.add(doc.get("key_1"));
        keys.add(doc.get("key_2"));
        emitter.emit(keys, null); 
    }   
}

My problem is that i need to query the view either only with key_1 or with a combination of key_1 and key_2 like so

List<Object> keys = new ArrayList<Object>();
keys.add(key_1);
if (key_2 != null) keys.add(key_2);
query.setKeys(keys);
results = query.run()

However the results are always empty. Do i overlook anything?


回答1:


Two emits doesn't work. If you give a ArrayList in setKeys() method, each key in the List match the each key in emit. If you want to match two keys, add keys ArrayList into another List. Then pass it to setKeys() method. Like this,

List<Object> keys = new ArrayList<Object>();
List<Object> allKeys = new ArrayList<Object>();
keys.add(key_1);
if (key_2 != null) keys.add(key_2);
allKeys.add(keys);
query.setKeys(allKeys);
results = query.run();



回答2:


This is the solution, We need to add keys into another List<Object> https://github.com/couchbase/couchbase-lite-android/issues/740



来源:https://stackoverflow.com/questions/31288941/query-multiple-keys-in-couchbase-lite-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!