Search code examples
couchdbcouchdb-nano

Nanodb replication filter


I have a db called: 'sample'

I have _design doc that look like:

{   "_id": "_design/orgFilter",
   "_rev":"9-7cd70d5ca52ddd42a3ff953c01d7cd36",
   "views": {
         "newView": {
           "map": "function(doc) {  
            if(doc.type && doc.type==='organization' ){
               emit(doc._id, doc.name);  
            }
                                 }"
                     }   
            },
    "language": "javascript"

}

And sample data like:

 {   "_id": "eb425729d084d49c8cddd45c6e00b07f",   "_rev":
 "2-9035a8eb3fa0fef44b0927bbe7090bbd",   "type": "organization",  
 "name": "org1" }

 {   "_id": "eb425729d084d49c8cddd45c6e00b2a7",   "_rev":
 "3-cf39cb747db01a4a71e58c2211de7c04",   "type": "organization",  
 "name": "org2" }

 {   "_id": "eb425729d084d49c8cddd45c6e00bf37",   "_rev":
 "3-d263492888a5755e91788901e59756ad",   "type": "organization",  
 "name": "org3" }

In my node server i using nano db and try use his replication function with filter , it's look like:

 nano.db.replicate('sample','http://localhost:5984/'+'b_rep',
            {create_target: true,continuous:true,filter:"orgFilter/newView"} , function (err, body) {

              if (!err)
                console.log(body);
            });

The response of this opriation is : "missing json key: newView"

What should i need to do to make this filter replication?

Thanks


Solution

  • You can't use a view as a filter.

    Here's a filter example :

    {
       "_id": "_design/orgFilter",
       "_rev": "6-6574469635168a0ae817df513ac4311d",
       "language": "javascript",
       "filters": {
           "organization": "function(doc,req){return doc.type && doc.type==='organization';}"
       }
    }
    

    Notice that you need a _design documents and you need to define your filters under the property filters.

    Basically, a filter function needs to returns true or false. True meaning that the replication will occur. For more detailed information, visit this link.