如何使用c#应用过滤器Couchbase服务器

时间:2017-06-08 04:23:09

标签: c# .net couchdb

这是我的json数据

{
"_id":"biking",
"_rev":"AE19EBC7654",
"type":"user",
"body":"My biggest hobby is mountainbiking. The other day...",
"date":"2009/01/30 18:04:11"
}
{
"_id":"biking",
"_rev":"AE19EBC7654",
"type":"testuser",
"body":"My biggest hobby is mountainbiking. The other day...",
"date":"2009/01/30 18:04:11"
}

以下是迄今为止所尝试的内容

var pull = _db.CreatePullReplication(syncGatewayUri);
var push = _db.CreatePushReplication(syncGatewayUri);

 _db.SetFilter("byUser", (revision, filterParams) =>
{
var typeParam = filterParams["type"].ToString();

return (typeParam != null) && typeParam.Equals("USer");
});

pull.Filter ="byUser";

我想获得仅包含用户类型的结果。但我无法应用过滤器。

1 个答案:

答案 0 :(得分:1)

因此,如果您只想为用户创建过滤器,则不需要参数。如果文档的类型是“user”,您要做的是返回true。

以下是一个例子:

var pull = _db.CreatePullReplication(syncGatewayUri);
var push = _db.CreatePushReplication(syncGatewayUri);

 _db.SetFilter("byUser", (revision, filterParams) =>
{
  //We get the type property
  var docType = (string)revision.GetProperty("type");
  //We make sure it's a user
  return !String.IsNullOrEmpty(docType) && docType.toLowerCase() == "user";
});

pull.Filter ="byUser";

如果您想要更加动态,可以创建一个byType过滤器并指定“user”类型参数。

示例:

var pull = _db.CreatePullReplication(syncGatewayUri);
var push = _db.CreatePushReplication(syncGatewayUri);

 _db.SetFilter("byType", (revision, filterParams) =>
{
var typeParam = filterParams["type"].ToString();
var docType = (string)revision.GetProperty("type");
return (typeParam != null) && !String.isNullOrEmpty(docType) && typeParam.toLowerCase() == docType.toLowerCase();
})

pull.Filter ="byType";
pull.FilterParams = new Dictionary<string, object> { {"type", "user"} };

有关详细信息,请参阅CouchBase文档。