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

How to Apply Filter Couchbase Server Using c#

这是我的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";

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

因此,如果您只想为用户创建过滤器,则不需要参数。如果文档的类型是 "user",您想要做的是 return 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 文档。