android - 如何在Android中的Couchbase-lite中实现 "WHERE"条件

标签 android couchbase-lite

我想查找特定键具有相同给定值的所有文档。在 SQL 中,这可以使用 WHERE 子句来实现。在 Mongo 中,我可以使用 find()。但我不确定如何在 Android 的 Couchbase-lite 中实现这一点。如果有人可以帮助解决这个问题,那就太好了。谢谢。

最佳答案

这就是我解决问题的方法。

创建和初始化 View 的代码:

com.couchbase.lite.View dateTimeView = database.getView("dateTimeView");
dateTimeView.setMap(new Mapper() {
    @Override
    public void map(Map<String, Object> document, Emitter emitter) {
        String dateTime = (String) document.get(QuickstartPreferences.DATE_TIME); // String for Key
        emitter.emit(dateTime, null);        
    }
}, "1");

查询上述 View 的代码:

Query query = database.getView("dateTimeView").createQuery();
query.setLimit(100);
query.setStartKey(date_time); //to retreive records for a given date_time value
query.setEndKey(date_time); 
QueryEnumerator result = null;
try {
    result = query.run();    
    for (Iterator<QueryRow> it = result; it.hasNext(); ) {
        QueryRow row = it.next();
        <String, Object> map = row.getDocument().getProperties();
        String employeeName = map.get(QuickstartPreferences.EMPLOYEE_NAME).toString(); // String for Key
        if(employeeName.equalsIgnoreCase(employee_name)) {
            // other logic
        }
    }
} catch (CouchbaseLiteException e) {
    e.printStackTrace();
}

这里重要的一点是,当您为 View 设置Map时,查询字段的值必须设置为Key(而不是Value)。 可以为null。然后,您可以轻松使用 setStartKeysetEndKey 函数。

关于android - 如何在Android中的Couchbase-lite中实现 "WHERE"条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37641164/

相关文章:

android - setfitsystemwindows not working windowtranslucentstatus coordinatorlayout

android - 通过 openshift 服务器在移动设备上出现 https 安全证书不可信错误

couchbase - 同步并删除从 CouchDB 和 Couchbase-lite 中删除的文档

android - 具有复制功能的移动本地数据库(如 Couchbase lite、Parse.com 或 Claudant)可以替代 Web 服务层吗?

xamarin.forms - 自己实现和托管 couchbase lite/mobile 的价格是多少

android - 程序类型已经存在 : com. google.common.util.concurrent.internal.InternalFutureFailureAccess

java - 如果我在 RecyclerView 项目中选择一个按钮,按钮会自动单击

android - 'for loop' 中的数据排列错误

java - CouchBase-lite - 无法应用管理器中的上下文

ionic-framework - Ionic Couchbase Lite Karma Jasmine 单元测试