{"id":2129,"date":"2017-05-29T13:18:32","date_gmt":"2017-05-29T13:18:32","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=2129"},"modified":"2018-10-10T17:15:56","modified_gmt":"2018-10-10T17:15:56","slug":"couchdb-view-emitting-the-same-document-twice","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=2129","title":{"rendered":"CouchDB View emitting the same document twice"},"content":{"rendered":"<p>I had a bug whereby my view was emitting the same place document twice. This was strange as the database clearly did not contain 2 copies of the document. I had seen what appeared to be a similar issue before bud at the time did not track down the cause.<\/p>\n<p>It turned out to be a bug in the view definition. The original (buggy) version follows:-<\/p>\n<blockquote>\n<p>function (doc) {     <br \/>&#160; if (doc.type == &#8216;place&#8217;) {      <br \/>&#160;&#160;&#160;&#160;&#160; emit([doc.address.postTown, doc.address.locality, doc.name, doc._id, 0], null);      <br \/>&#160;&#160;&#160;&#160;&#160; emit([doc.address.postTown, doc.address.locality, doc.name, doc._id, 1], {_id: doc.placeFeaturesId});      <br \/>&#160; }      <br \/>}<\/p>\n<\/blockquote>\n<p>The problem with this is that when doc.placeFeaturesId is null\/undefined, the second emit emits its key plus an \u201c_id\u201d with an undefined\/null value in the generated json. The result of this is that when using ?include_docs=true to include the documents, CouchDB applies a default behaviour due to the null, and emits the place document again a second time. The following fix prevents the second emit when there are no placeFeatures present, which prevents the place document being emitted a second time. This solves the problem:-<\/p>\n<blockquote>\n<p>function (doc) {     <br \/>&#160; if (doc.type == &#8216;place&#8217;) {      <br \/>&#160;&#160;&#160;&#160;&#160; emit([doc.address.postTown, doc.address.locality, doc.name, doc._id, 0], null);      <br \/>&#160;&#160;&#160;&#160;&#160; if (doc.placeFeaturesId) {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; emit([doc.address.postTown, doc.address.locality, doc.name, doc._id, 1], {_id: doc.placeFeaturesId});      <br \/>&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160; }      <br \/>}<\/p>\n<\/blockquote>\n<p>The lesson here is to be careful to handle undefined or null values in the view code when emitting entries in a view definition as this can cause strange behaviour such as the duplicates above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a bug whereby my view was emitting the same place document twice. This was strange as the database clearly did not contain 2 copies of the document. I had seen what appeared to be a similar issue before bud at the time did not track down the cause. It turned out to be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[197],"tags":[13,202,40,16],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2129"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2129"}],"version-history":[{"count":4,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2129\/revisions"}],"predecessor-version":[{"id":2133,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2129\/revisions\/2133"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}