从CouchDB中过滤复制的Couchbase Lite拉取 [英] Couchbase Lite pull with filtered replication from CouchDB

查看:135
本文介绍了从CouchDB中过滤复制的Couchbase Lite拉取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有设法从CouchDB中提取复制数据。

I did not manage to pull replication data from CouchDB.

我将 CouchDB 用于文档存储。

CouchDB版本: Apache CouchDB 1.6.1

Couchbase Lite: Couchbase Lite 1.4.0

CouchDB Version: Apache CouchDB 1.6.1
Couchbase Lite: Couchbase Lite 1.4.0

以下是我的示例数据:

{
   "_id": "ab33deeb074523e3c63c216b8c2952a8",
   "_rev": "6-e196bfb6aca85492e4f96f3af6fd1ee2",
   "type": "employee",
   "employee": {
       "employeeId": "1",
       "employeeName": "Test"
   }
 }

CouchDB 中,我创建了自己的设计过滤器:

In CouchDB, I have created my own design filter:

{
   "_id": "_design/employee",
   "_rev": "35-00f59706402452291d30c3fb6e9a5356",
   "filters": {
       "byEmployeeId": "function(doc, req) {
                            if(doc.type != 'employee') {
                                return false;
                            } 

                             if(doc.employee.employeeId == req.query.employeeId) {
                                return true;
                            } else {
                                return false;
                            }
                        }"
   }
}

另一方面,我将 Couchbase Lite 用作我的Android Mobile项目来复制员工数据:

On the other hand, I am using Couchbase Lite as my Android Mobile project to replication pull the employee data:

Replication pull = this.getCouchbaseUtility().getDatabase().createPullReplication(
                    new URL("http://localhost:5984/testdb")
            );

            pull.setAuthenticator(authenticator);
            pull.setContinuous(false);

            pull.setFilter("employee/byEmployeeId");

            Map<String, Object> params = new HashMap<>();
            params.put("employeeId", "1");

            pull.setFilterParams(params);

            pull.addChangeListener(new Replication.ChangeListener() {
                @Override
                public void changed(Replication.ChangeEvent event) {
                    System.out.println(event.getStatus());
                }
            });

            pull.stop();
            pull.start();

该应用程序没有错误运行,但没有成功将数据复制到移动Couchbase存储中。

The App runs with no error, but it did not successfully replicate the data to the mobile Couchbase storage.

如果我将设计过滤器更改为始终返回true,则App可以从CouchDB复制员工文档。可能是 req.query.employeeId Couchbase CouchDB 不兼容吗?

If I change the design filter to return always true, App can replicate the employee document from CouchDB. Could it be req.query.employeeId not compatible with Couchbase and CouchDB?

我可以知道我做错了什么吗?

May I know any part that I did wrong?

推荐答案

在查询字符串中使用过滤器参数不是正确的实现方式查询字符串有其自身的局限性。如果您使用的是CouchDB Server 2.0及更高版本,则应该使用选择器而不是过滤器。您唯一需要确保在客户端的是设置过滤器名称= _selector,然后在过滤器参数中以以下格式传递参数-

Using filter params in query string is not a correct way to implement as query string has its own limitation. Instead one should use selector instead of filter if you are using CouchDB Server 2.0 and above. Only thing you need to make sure at your client side is, set the filter name = _selector and in filter params, pass params in following format -

{
  "selector": {
    "key_name_to_filter": "key_value_to_filter",
    "$or": [
      {
        "$and": [
          {
            "key_1": "value_1"
          },
          {
            "key_2": "value2"
          }
        ]
      },
      {
        "$and": [
          {
            "key_3": "value_3"
          },
          {
            "key_4": "value_4"
          }
        ]
      }
    ]
  }
}

上面的格式只是一个示例,说明您可以在选择器过滤器中创建复杂的查询。

Above format is just an example illustrating you can create complex queries in selector filter.

这篇关于从CouchDB中过滤复制的Couchbase Lite拉取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆