CouchDB filtered replication - remove a document

前端 未结 2 1008
野性不改
野性不改 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.

    0 讨论(0)
  • 2021-02-09 19:17

    This is a tricky subject. The options we have considered are:

    1. When the group changes copy the document and save the new group to the copy, then delete the original. The delete will propagate through the filter and remove from the remote DB. Downsides are loss of revisions and breakage if the ID was referenced from other docs.
    2. Have the user database write a manifest file listing all the docs in the DB, which syncs back to the master (we had this already to track document delivery, and read status). Compare that to the intended (filtered) document list and tell the user database to PURGE (not DELETE) the docs that should no longer be present.

    Best reference I have found is here: http://pouchdb.com/2015/04/05/filtered-replication.html

    0 讨论(0)
提交回复
热议问题