CouchDB过滤的复制-删除文档 [英] CouchDB filtered replication - remove a document

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

问题描述

我正在尝试在主数据库和用户数据库之间设置过滤的复制。
主文档中的文档包含有权使用该文档的用户组列表。

I am trying to setup filtered replication between a master and user database. Documents in the master contain a list of user groups that have permission to the document.

{
  _id: 'one',
  groups: ['a', 'b']
}

{
  _id: 'two',
  groups: ['c', 'd']
}

我创建了以下内容的过滤视图仅允许该组用户获取复制文档副本的数据库(在此示例中,硬编码 a组)

I created a filtered view of the database that only allows user with the group to get a copy of the replicated document (hard coding the group of 'a' in this example)

{
  filters = {
    users = function(doc, req){
      return doc.groups.indexOf(req.query.group) != -1;
    }
  }
}

我然后创建一个复制文档_replicator数据库中

I then create a replication document in the _replicator database

{
  source: "master",
  target: "user1",
  filter: "replication/user",
  query_params: {group: "a"},
  create_target: true
}

创建此文档后,复制开始,并且将文档 one从主复制到user1。

Once this document is created replication begins and the document 'one' is replicated from master to user1. Document 'two' is not replicated - just what I want.

随后用户从组 a移动到组 c,因此我创建了一个新的复制文档:

Subsequently the user is moved from group 'a' to group 'c' so I create a new replication document:

{
  source: "master",
  target: "user1",
  filter: "replication/user",
  query_params: {group: "c"},
  create_target: true
}

我想要的行为是要从用户数据库中删除文档一,并复制文档二。碰巧的是,文件一个仍然存在,文件两个被复制了。显然,除非在源数据库中删除了文档,否则复制过滤器不允许在目标数据库中进行删除。

The behaviour I want is for document 'one' to be removed from the user database and for document 'two' to be replicated. As it happens document 'one' remains and document 'two' is replicated. Obviously the replication filter does not allow for deletions in the target database unless the document is deleted in the source database.

那么该如何处理?还是我应该考虑使用其他结构?

How then should this scenario be handled? Or is there an alternate structure I should be considering?

推荐答案

据我所知,没有办法使用复制来修改文​​档。但是,您可以采用两种方法。

As far as I know there is no way to modify documents using replication. But there are two approaches that you can take.

首先,您可以创建一个新数据库并将其复制到该数据库。例如,如果您的查询参数更改为 user c ,而不是将其复制到 user1 ,则创建另一个数据库某个名称并复制到该名称,然后删除原始数据库(或保留该数据库,以防再次查询参数更改)。您甚至可以为源数据库使用描述性名称,例如 user1_filter_a。这是最省事的方法,但是如果文档数量很大且重叠(例如很多用户b属于a和c组)并且复制器过滤器快速更改,则效率可能很低。

First you can create a new database and replicate to it. For instance if your query parameter changes to user c instead of replicating it to user1 create another database some name and replicate to it and then delete the original database (or keep it in case you query param changes again). You can even use descriptive names for your source database like "user1_filter_a". This is the most hassle free way to do it but if the number of documents is large and overlapping (like lots of user b's that belong to both groups a and c) and your replicator filter changes rapidly it can be inefficient.

另一种方法是使用视图批量文档api 。首先根据这样的组字段创建一个视图,使发出文档

Another way is to use the view and Bulk document api. First create a view that emits the document based on the group field like so

function map(doc){

     emit(doc.groups,doc._id);

  } 

然后用

startkey = [ a]& endKey = [ a,{}]& include_docs = true

获取要删除的所有文档。然后遍历结果集,并将
doc._deleted = true 附加到每个文档,然后对数据库进行批量请求。所有文档都将被删除(更多说明此处)。这种方法的优点是可以保留一个数据库。

to get all the documents that you want to delete. Then iterate over the result set and append doc._deleted=true to each document and make a bulk request to the database. All the documents will be deleted (more explanation here) .The advantage of this method is that you can keep a single database.

简而言之,如果要保留一个数据库,则必须手动删除文档。但是,如果您对基于复制器功能的多个数据库开放,则每次过滤器更改时都可以创建一个新数据库。

In short if you want to keep a single database you will have to delete the docs manually. But if you are open to multiple database based on replicator function you can just create a new database every time your filter changes.

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

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