You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by John Le Brasseur <jo...@gmail.com> on 2017/02/08 08:23:40 UTC

Filtered Replication

Hi.
Can anyone tell me why this works:

{
  "_id": "_design/first_filter",
  "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
  "filters": {
    "filter1": "function(doc, req) {if (doc.abc === \"xyz\") {return
true;}else{return false;}}"
  },
  "language": "javascript"
}



and this does not:

{
  "_id": "_design/first_filter",
  "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
  "filters": {
    "filter1": "function(doc, req) {if (doc.def.ghi === \"jkl\") {return
true;}else{return false;}}"
  },
  "language": "javascript"
}


In the second case I get:

[error] 2017-02-07T18:55:40.384000Z couchdb@localhost <0.32487.361>
f201ca664a OS Process Error <0.18298.118> ::
{<<"TypeError">>,{[{<<"message">>,<<"doc.def is undefined">>}



I'm using v2.0.0 on Windows and running a filtered replication using curl.

The documents contain the relevant structure and all documents contain
"abc", and "def.ghi" and have values.

Many thanks.

John

Re: Filtered Replication

Posted by John Le Brasseur <jo...@gmail.com>.
Hi James.
I forgot to include 'typeof'.
Did as you said and works like a bomb.
I tried it with a couple of different options including numerical
filters(>=10, <10) and everything worked fine.
Thank you for your assistance. This opens up wonderful options for creating
different (sub-)databases.




On 8 February 2017 at 13:06, James Dingwall <ja...@zynstra.com>
wrote:

> On 08/02/17 10:53, John Le Brasseur wrote:
>
>> Hi James.
>> Thank you for the suggestions.
>> Have tried permutations of your suggestions.
>> No change.
>> As soon as the function contains nested json(doc.def.ghi) it fails to
>> work.
>>
>> Complete error message:
>> [error] 2017-02-08T10:31:46.026000Z couchdb@localhost <0.11919.372>
>> f0465901da rexi_server
>> throw:{<<"TypeError">>,{[{<<"message">>,<<"doc.plantation is
>> undefined">>},{<<"fileName">>,<<"filters.filter1">>},{<<"lin
>> eNumber">>,1},{<<"stack">>,<<"([object
>> Object],[object Object])@filters.filter1:1\n(function (doc, req) {if (
>> doc.plantation.name && doc.plantation.name === \"Sunset View\") {return
>> true;} else {return false;}},[object Object],[object
>> Array])@./share/server/main.js:509\n(\"_design/first_filter\",[object
>> Array],[object
>> Array])@./share/server/main.js:1526\n()@./share/server/main.
>> js:1571\n()@./share/server/main.js:1592\n@./share/server/mai
>> n.js:1\n">>}]}}
>> [{couch_os_process,prompt,2,[{file,"src/couch_os_process.erl
>> "},{line,59}]},{couch_query_servers,proc_prompt,2,[{file,"sr
>> c/couch_query_servers.erl"},{line,427}]},{couch_query_serve
>> rs,with_ddoc_proc,2,[{file,"src/couch_query_servers.erl"},
>> {line,421}]},{couch_query_servers,filter_docs,5,[{file,"
>> src/couch_query_servers.erl"},{line,405}]},{couch_changes,
>> filter,3,[{file,"src/couch_changes.erl"},{line,295}]},{
>> fabric_rpc,changes_enumerator,2,[{file,"src/fabric_rpc.erl"}
>> ,{line,336}]},{couch_btree,stream_kv_node2,8,[{file,"src/
>> couch_btree.erl"},{line,783}]},{couch_btree,stream_kp_node,
>> 8,[{file,"src/couch_btree.erl"},{line,754}]}]
>>
>
>
> Your new test seems to be
>
> if (doc.plantation.name && doc.plantation.name === \"Sunset View\")
>
> Which still immediately tries to access a key the doc.plantation object
> but that causes an error as soon as it is not an object or is absent.
> My suggested test
>
> doc.plantation - there is a value at doc.plantation
> typeof doc.plantation === 'object' - that it is an object (null would
> evaluate as an object hence the previous test which fails for null or
> undefined)
> doc.plantation.name === \"Sunset View\" - the interesting value
>
> otherwise you could write the test as a try/catch block
>
> try {
>     if(doc.plantation.name !== 'Sunset View') {
>         throw {};
>     }
>     return true;
> }
> catch(e) {
>     return false;
> }
>
> James
>
> (Sorry about the auto-appended disclaimer btw)
>
>
>
>>
>> On 8 February 2017 at 10:50, James Dingwall <ja...@zynstra.com>
>> wrote:
>>
>>
>>>
>>> On 08/02/17 08:23, John Le Brasseur wrote:
>>>
>>> Hi.
>>>> Can anyone tell me why this works:
>>>>
>>>> {
>>>>   "_id": "_design/first_filter",
>>>>   "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
>>>>   "filters": {
>>>>     "filter1": "function(doc, req) {if (doc.abc === \"xyz\") {return
>>>> true;}else{return false;}}"
>>>>   },
>>>>   "language": "javascript"
>>>> }
>>>>
>>>>
>>>>
>>>> and this does not:
>>>>
>>>> {
>>>>   "_id": "_design/first_filter",
>>>>   "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
>>>>   "filters": {
>>>>     "filter1": "function(doc, req) {if (doc.def.ghi === \"jkl\") {return
>>>> true;}else{return false;}}"
>>>>   },
>>>>   "language": "javascript"
>>>> }
>>>>
>>>>
>>>> In the second case I get:
>>>>
>>>> [error] 2017-02-07T18:55:40.384000Z couchdb@localhost <0.32487.361>
>>>> f201ca664a OS Process Error <0.18298.118> ::
>>>> {<<"TypeError">>,{[{<<"message">>,<<"doc.def is undefined">>}
>>>>
>>>>
>>> Design documents can be considered by the replication depending on
>>> configuration and the example you have given does not have doc.def.
>>> Perhaps rewrite the test to make sure doc.def is present and is an
>>> object.
>>>
>>> if(doc.def && typeof doc.def === 'object' && doc.def.ghi === 'jkl'){}
>>> Zynstra is a private limited company registered in England and Wales
>>> (registered number 07864369). Our registered office and Headquarters are
>>> at
>>> The Innovation Centre, Broad Quay, Bath, BA1 1UD. This email, its
>>> contents
>>> and any attachments are confidential. If you have received this message
>>> in
>>> error please delete it from your system and advise the sender
>>> immediately.
>>>
>>>
>> Zynstra is a private limited company registered in England and Wales
> (registered number 07864369). Our registered office and Headquarters are at
> The Innovation Centre, Broad Quay, Bath, BA1 1UD. This email, its contents
> and any attachments are confidential. If you have received this message in
> error please delete it from your system and advise the sender immediately.
>

Re: Filtered Replication

Posted by James Dingwall <ja...@zynstra.com>.
On 08/02/17 10:53, John Le Brasseur wrote:
> Hi James.
> Thank you for the suggestions.
> Have tried permutations of your suggestions.
> No change.
> As soon as the function contains nested json(doc.def.ghi) it fails to work.
>
> Complete error message:
> [error] 2017-02-08T10:31:46.026000Z couchdb@localhost <0.11919.372>
> f0465901da rexi_server
> throw:{<<"TypeError">>,{[{<<"message">>,<<"doc.plantation is
> undefined">>},{<<"fileName">>,<<"filters.filter1">>},{<<"lineNumber">>,1},{<<"stack">>,<<"([object
> Object],[object Object])@filters.filter1:1\n(function (doc, req) {if (
> doc.plantation.name && doc.plantation.name === \"Sunset View\") {return
> true;} else {return false;}},[object Object],[object
> Array])@./share/server/main.js:509\n(\"_design/first_filter\",[object
> Array],[object
> Array])@./share/server/main.js:1526\n()@./share/server/main.js:1571\n()@./share/server/main.js:1592\n@./share/server/main.js:1\n">>}]}}
> [{couch_os_process,prompt,2,[{file,"src/couch_os_process.erl"},{line,59}]},{couch_query_servers,proc_prompt,2,[{file,"src/couch_query_servers.erl"},{line,427}]},{couch_query_servers,with_ddoc_proc,2,[{file,"src/couch_query_servers.erl"},{line,421}]},{couch_query_servers,filter_docs,5,[{file,"src/couch_query_servers.erl"},{line,405}]},{couch_changes,filter,3,[{file,"src/couch_changes.erl"},{line,295}]},{fabric_rpc,changes_enumerator,2,[{file,"src/fabric_rpc.erl"},{line,336}]},{couch_btree,stream_kv_node2,8,[{file,"src/couch_btree.erl"},{line,783}]},{couch_btree,stream_kp_node,8,[{file,"src/couch_btree.erl"},{line,754}]}]


Your new test seems to be

if (doc.plantation.name && doc.plantation.name === \"Sunset View\")

Which still immediately tries to access a key the doc.plantation object
but that causes an error as soon as it is not an object or is absent.
My suggested test

doc.plantation - there is a value at doc.plantation
typeof doc.plantation === 'object' - that it is an object (null would
evaluate as an object hence the previous test which fails for null or
undefined)
doc.plantation.name === \"Sunset View\" - the interesting value

otherwise you could write the test as a try/catch block

try {
     if(doc.plantation.name !== 'Sunset View') {
         throw {};
     }
     return true;
}
catch(e) {
     return false;
}

James

(Sorry about the auto-appended disclaimer btw)

>
>
> On 8 February 2017 at 10:50, James Dingwall <ja...@zynstra.com>
> wrote:
>
>>
>>
>> On 08/02/17 08:23, John Le Brasseur wrote:
>>
>>> Hi.
>>> Can anyone tell me why this works:
>>>
>>> {
>>>   "_id": "_design/first_filter",
>>>   "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
>>>   "filters": {
>>>     "filter1": "function(doc, req) {if (doc.abc === \"xyz\") {return
>>> true;}else{return false;}}"
>>>   },
>>>   "language": "javascript"
>>> }
>>>
>>>
>>>
>>> and this does not:
>>>
>>> {
>>>   "_id": "_design/first_filter",
>>>   "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
>>>   "filters": {
>>>     "filter1": "function(doc, req) {if (doc.def.ghi === \"jkl\") {return
>>> true;}else{return false;}}"
>>>   },
>>>   "language": "javascript"
>>> }
>>>
>>>
>>> In the second case I get:
>>>
>>> [error] 2017-02-07T18:55:40.384000Z couchdb@localhost <0.32487.361>
>>> f201ca664a OS Process Error <0.18298.118> ::
>>> {<<"TypeError">>,{[{<<"message">>,<<"doc.def is undefined">>}
>>>
>>
>> Design documents can be considered by the replication depending on
>> configuration and the example you have given does not have doc.def.
>> Perhaps rewrite the test to make sure doc.def is present and is an object.
>>
>> if(doc.def && typeof doc.def === 'object' && doc.def.ghi === 'jkl'){}
>> Zynstra is a private limited company registered in England and Wales
>> (registered number 07864369). Our registered office and Headquarters are at
>> The Innovation Centre, Broad Quay, Bath, BA1 1UD. This email, its contents
>> and any attachments are confidential. If you have received this message in
>> error please delete it from your system and advise the sender immediately.
>>
>
Zynstra is a private limited company registered in England and Wales (registered number 07864369). Our registered office and Headquarters are at The Innovation Centre, Broad Quay, Bath, BA1 1UD. This email, its contents and any attachments are confidential. If you have received this message in error please delete it from your system and advise the sender immediately.

Re: Filtered Replication

Posted by John Le Brasseur <jo...@gmail.com>.
Hi James.
Thank you for the suggestions.
Have tried permutations of your suggestions.
No change.
As soon as the function contains nested json(doc.def.ghi) it fails to work.

Complete error message:
[error] 2017-02-08T10:31:46.026000Z couchdb@localhost <0.11919.372>
f0465901da rexi_server
throw:{<<"TypeError">>,{[{<<"message">>,<<"doc.plantation is
undefined">>},{<<"fileName">>,<<"filters.filter1">>},{<<"lineNumber">>,1},{<<"stack">>,<<"([object
Object],[object Object])@filters.filter1:1\n(function (doc, req) {if (
doc.plantation.name && doc.plantation.name === \"Sunset View\") {return
true;} else {return false;}},[object Object],[object
Array])@./share/server/main.js:509\n(\"_design/first_filter\",[object
Array],[object
Array])@./share/server/main.js:1526\n()@./share/server/main.js:1571\n()@./share/server/main.js:1592\n@./share/server/main.js:1\n">>}]}}
[{couch_os_process,prompt,2,[{file,"src/couch_os_process.erl"},{line,59}]},{couch_query_servers,proc_prompt,2,[{file,"src/couch_query_servers.erl"},{line,427}]},{couch_query_servers,with_ddoc_proc,2,[{file,"src/couch_query_servers.erl"},{line,421}]},{couch_query_servers,filter_docs,5,[{file,"src/couch_query_servers.erl"},{line,405}]},{couch_changes,filter,3,[{file,"src/couch_changes.erl"},{line,295}]},{fabric_rpc,changes_enumerator,2,[{file,"src/fabric_rpc.erl"},{line,336}]},{couch_btree,stream_kv_node2,8,[{file,"src/couch_btree.erl"},{line,783}]},{couch_btree,stream_kp_node,8,[{file,"src/couch_btree.erl"},{line,754}]}]


On 8 February 2017 at 10:50, James Dingwall <ja...@zynstra.com>
wrote:

>
>
> On 08/02/17 08:23, John Le Brasseur wrote:
>
>> Hi.
>> Can anyone tell me why this works:
>>
>> {
>>   "_id": "_design/first_filter",
>>   "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
>>   "filters": {
>>     "filter1": "function(doc, req) {if (doc.abc === \"xyz\") {return
>> true;}else{return false;}}"
>>   },
>>   "language": "javascript"
>> }
>>
>>
>>
>> and this does not:
>>
>> {
>>   "_id": "_design/first_filter",
>>   "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
>>   "filters": {
>>     "filter1": "function(doc, req) {if (doc.def.ghi === \"jkl\") {return
>> true;}else{return false;}}"
>>   },
>>   "language": "javascript"
>> }
>>
>>
>> In the second case I get:
>>
>> [error] 2017-02-07T18:55:40.384000Z couchdb@localhost <0.32487.361>
>> f201ca664a OS Process Error <0.18298.118> ::
>> {<<"TypeError">>,{[{<<"message">>,<<"doc.def is undefined">>}
>>
>
> Design documents can be considered by the replication depending on
> configuration and the example you have given does not have doc.def.
> Perhaps rewrite the test to make sure doc.def is present and is an object.
>
> if(doc.def && typeof doc.def === 'object' && doc.def.ghi === 'jkl'){}
> Zynstra is a private limited company registered in England and Wales
> (registered number 07864369). Our registered office and Headquarters are at
> The Innovation Centre, Broad Quay, Bath, BA1 1UD. This email, its contents
> and any attachments are confidential. If you have received this message in
> error please delete it from your system and advise the sender immediately.
>

Re: Filtered Replication

Posted by James Dingwall <ja...@zynstra.com>.

On 08/02/17 08:23, John Le Brasseur wrote:
> Hi.
> Can anyone tell me why this works:
>
> {
>   "_id": "_design/first_filter",
>   "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
>   "filters": {
>     "filter1": "function(doc, req) {if (doc.abc === \"xyz\") {return
> true;}else{return false;}}"
>   },
>   "language": "javascript"
> }
>
>
>
> and this does not:
>
> {
>   "_id": "_design/first_filter",
>   "_rev": "24-a6c1822c543f96d1bf4803112092e9ae",
>   "filters": {
>     "filter1": "function(doc, req) {if (doc.def.ghi === \"jkl\") {return
> true;}else{return false;}}"
>   },
>   "language": "javascript"
> }
>
>
> In the second case I get:
>
> [error] 2017-02-07T18:55:40.384000Z couchdb@localhost <0.32487.361>
> f201ca664a OS Process Error <0.18298.118> ::
> {<<"TypeError">>,{[{<<"message">>,<<"doc.def is undefined">>}

Design documents can be considered by the replication depending on
configuration and the example you have given does not have doc.def.
Perhaps rewrite the test to make sure doc.def is present and is an object.

if(doc.def && typeof doc.def === 'object' && doc.def.ghi === 'jkl'){}
Zynstra is a private limited company registered in England and Wales (registered number 07864369). Our registered office and Headquarters are at The Innovation Centre, Broad Quay, Bath, BA1 1UD. This email, its contents and any attachments are confidential. If you have received this message in error please delete it from your system and advise the sender immediately.