CouchDB filtered replication - remove a document

前端 未结 2 1007
野性不改
野性不改 2021-02-09 18:52

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.

2条回答
  •  既然无缘
    2021-02-09 19:03

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

    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.

    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);
    
      } 
    

    and then query with

    startkey=["a"]&endKey=["a",{}]&include_docs=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.

提交回复
热议问题