[Couchdb Wiki] Update of "View_Snippets" by JoanTouzet
Dear wiki user, You have subscribed to a wiki page "Couchdb Wiki" for change notification. The page "View_Snippets" has been deleted by JoanTouzet: https://wiki.apache.org/couchdb/View_Snippets?action=diff=48=49 - <> - Please see our [[http://docs.couchdb.org/en/stable/ddocs/index.html|official documentation]] instead. - - (All of the examples formerly on this page are better suited for the new [[http://docs.couchdb.org/en/stable/api/database/find.html|mango-based queries]], anyway.) -
[Couchdb Wiki] Update of "View_Snippets" by JoanTouzet
Dear Wiki user, You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification. The "View_Snippets" page has been changed by JoanTouzet: https://wiki.apache.org/couchdb/View_Snippets?action=diff=47=48 Please see our [[http://docs.couchdb.org/en/stable/ddocs/index.html|official documentation]] instead. - (All of the examples formerly on this page are better suited for the new [[http://docs.couchdb.org/en/2.1.1/api/database/find.html|mango-based queries]], anyway.) + (All of the examples formerly on this page are better suited for the new [[http://docs.couchdb.org/en/stable/api/database/find.html|mango-based queries]], anyway.)
[Couchdb Wiki] Update of "View_Snippets" by JoanTouzet
Dear Wiki user, You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification. The "View_Snippets" page has been changed by JoanTouzet: https://wiki.apache.org/couchdb/View_Snippets?action=diff=46=47 Comment: Removing obsolete and not-recommended advice; point people to Mango instead <> + Please see our [[http://docs.couchdb.org/en/stable/ddocs/index.html|official documentation]] instead. - = View Snippets = - < > - This page collects code snippets to be used in your [[Views]]. They are mainly meant to help get your head around the map/reduce approach to accessing database content. + (All of the examples formerly on this page are better suited for the new [[http://docs.couchdb.org/en/2.1.1/api/database/find.html|mango-based queries]], anyway.) - '''Remember''': the Futon web client silently adds ''group=true'' to your views. - - == Common mistakes == - When creating a reduce function, a re-reduce should behave in the same way as the regular reduce. The reason is that CouchDB doesn't necessarily call re-reduce on your map results. - - Think about it this way: If you have a bunch of values ''V1 V2 V3'' for key ''K'', then you can get the combined result either by calling ''reduce([K,K,K],[V1,V2,V3],0)'' or by re-reducing the individual results: ''reduce(null,[R1,R2,R3],1)''. This depends on what your view results look like internally. - - == Get docs with a particular user id == - {{{#!highlight javascript - // map - function(doc) { - if (doc.user_id) { - emit(doc.user_id, null); - } - } - }}} - Then query with ''key=USER_ID'' to get all the rows that match that user. - - == Get all documents which have an attachment == - This lists only the documents which have an attachment. - - {{{#!highlight javascript - // map - function(doc) { - if (doc._attachments) { - emit(doc._id, null); - } - } - }}} - In SQL this would be something like {{{SELECT id FROM table WHERE attachment IS NOT NULL}}}. - - == Count documents with and without an attachment == - Call this with ''group=true'' or you only get the combined number of documents with and without attachments. - - {{{#!highlight javascript - // map - function(doc) { - if (doc._attachments) { - emit("with attachment", 1); - } - else { - emit("without attachment", 1); - } - } - }}} - {{{#!highlight javascript - // reduce - function(keys, values) { -return sum(values); - } - }}} - Using curl you can call it like this: - - {{{ - curl -s -i -X POST -H 'Content-Type: application/json' - -d '{"map": "function(doc){if(doc._attachments) {emit(\"with\",1);} else {emit(\"without\",1);}}", - "reduce": "function(keys, values) {return sum(values);}"}' - 'http://localhost:5984/somedb/_temp_view?group=true' - }}} - In SQL this would be something along the lines of {{{SELECT num_attachments FROM table GROUP BY num_attachments}}} (but this would give extra output for rows containing more than one attachment). - - == Generating a list of unique values == - Here we use the fact that the key for a view result can be an array. Suppose you have a map that generates (key, value) pairs with many duplicates and you want to remove the duplicates. To do so, use {{{emit([key, value], null)}}} as the map output. - - Call this with ''group=true'' or you only get ''null''. - - {{{#!highlight javascript - // map - function(doc) { - for (var i in doc.links) - emit([doc.parent, i], null); - } - } - }}} - {{{#!highlight javascript - // reduce - function(keys, values) { -return null; - } - }}} - This will give you results like - - {{{#!highlight javascript - {"rows":[ - {"key":["thisparent","thatlink"],"value":null}, - {"key":["thisparent","thatotherlink"],"value":null} - ]} - }}} - You can then get all the rows for the key ''"thisparent"'' with the view parameters ''startkey=["thisparent"]=["thisparent",{}]_end=false'' - - Note that the trick here is using the key for what you want to make unique. You can combine this with the counting above to get a count of duplicate values: - - {{{#!highlight javascript - // map - function(doc) { - for (var i in doc.links) - emit([doc.parent, i], 1); - } - } - }}} - {{{#!highlight javascript - // reduce - function(keys, values) { -return sum(values); - } - }}} - If you then want to know the total count for each parent, you can use the ''group_level'' view parameter: ''startkey=["thisparent"]=["thisparent",{}]_end=false_level=1'' - - == Get contents of an object with specific attributes e.g. doc.objects.[0].attribute == - {{{#!highlight javascript - // map - function(doc) { - for (var idx in doc.objects) { - emit(doc.objects[idx], attribute) - } - } - }}} - == Arbitrarily query against any document property ("uniview") == - '''Note: This is not a generally recommended approach, as your view size will be (# of documents * # of document fields). '''However, if
[Couchdb Wiki] Update of View_Snippets by JoanTouzet
Dear Wiki user, You have subscribed to a wiki page or wiki category on Couchdb Wiki for change notification. The View_Snippets page has been changed by JoanTouzet: http://wiki.apache.org/couchdb/View_Snippets?action=diffrev1=45rev2=46 = View Snippets = TableOfContents() - This page collects code snippets to be used in your [[Views]]. They are mainly meant to help get your head around the map/reduce approach to accessing database content. + This page collects code snippets to be used in your [[Views]]. They are mainly meant to help get your head around the map/reduce approach to accessing database content. '''Remember''': the Futon web client silently adds ''group=true'' to your views. == Common mistakes == - When creating a reduce function, a re-reduce should behave in the same way as the regular reduce. The reason is that CouchDB doesn't necessarily call re-reduce on your map results. Think about it this way: If you have a bunch of values ''V1 V2 V3'' for key ''K'', then you can get the combined result either by calling ''reduce([K,K,K],[V1,V2,V3],0)'' or by re-reducing the individual results: ''reduce(null,[R1,R2,R3],1)''. This depends on what your view results look like internally. - == Get docs with a particular user id == - {{{#!highlight javascript // map function(doc) { @@ -26, +23 @@ } } }}} - Then query with ''key=USER_ID'' to get all the rows that match that user. - == Get all documents which have an attachment == - This lists only the documents which have an attachment. {{{#!highlight javascript @@ -42, +36 @@ } } }}} - In SQL this would be something like {{{SELECT id FROM table WHERE attachment IS NOT NULL}}}. - == Count documents with and without an attachment == - Call this with ''group=true'' or you only get the combined number of documents with and without attachments. {{{#!highlight javascript @@ -57, +48 @@ emit(with attachment, 1); } else { - emit(without attachment, 1); + emit(without attachment, 1); } } }}} - {{{#!highlight javascript // reduce function(keys, values) { return sum(values); } }}} - Using curl you can call it like this: {{{ - curl -s -i -X POST -H 'Content-Type: application/json' + curl -s -i -X POST -H 'Content-Type: application/json' - -d '{map: function(doc){if(doc._attachments) {emit(\with\,1);} else {emit(\without\,1);}}, + -d '{map: function(doc){if(doc._attachments) {emit(\with\,1);} else {emit(\without\,1);}}, - reduce: function(keys, values) {return sum(values);}}' + reduce: function(keys, values) {return sum(values);}}' 'http://localhost:5984/somedb/_temp_view?group=true' }}} - In SQL this would be something along the lines of {{{SELECT num_attachments FROM table GROUP BY num_attachments}}} (but this would give extra output for rows containing more than one attachment). - == Generating a list of unique values == - Here we use the fact that the key for a view result can be an array. Suppose you have a map that generates (key, value) pairs with many duplicates and you want to remove the duplicates. To do so, use {{{emit([key, value], null)}}} as the map output. Call this with ''group=true'' or you only get ''null''. @@ -95, +81 @@ } } }}} - {{{#!highlight javascript // reduce function(keys, values) { return null; } }}} + This will give you results like - This will give you results like {{{#!highlight javascript {rows:[ {key:[thisparent,thatlink],value:null}, {key:[thisparent,thatotherlink],value:null} ]} }}} - - You can then get all the rows for the key ''thisparent'' with the view parameters ''startkey=[''thisparent]endkey=[thisparent,{}]inclusive_end=false'' + You can then get all the rows for the key ''thisparent'' with the view parameters ''startkey=[thisparent]endkey=[thisparent,{}]inclusive_end=false'' Note that the trick here is using the key for what you want to make unique. You can combine this with the counting above to get a count of duplicate values: @@ -123, +107 @@ } } }}} - {{{#!highlight javascript // reduce function(keys, values) { return sum(values); } }}} + If you then want to know the total count for each parent, you can use the ''group_level'' view parameter: ''startkey=[thisparent]endkey=[thisparent,{}]inclusive_end=falsegroup_level=1'' - - If you then want to know the total count for each parent, you can use the ''group_level'' view parameter: - ''startkey=[''thisparent]endkey=[thisparent,{}]inclusive_end=falsegroup_level=1'' == Get contents of an object with specific attributes e.g. doc.objects.[0].attribute == - {{{#!highlight javascript // map function(doc) { @@ -144, +124 @@ } } }}} + == Arbitrarily query against any document property (uniview) == + '''Note: This is not a generally recommended approach, as your view size