Writing a simple group using map-reduce (Couchbase)

I am new to the whole concept of map reduction and I am trying to perform a simple map reduction function.

I am currently working with a Couchbase server as a NoSQL database.

I want to get a list of all my types:

key: 1, value: null key: 2, value: null key: 3, value: null 

Here are my docs:

 { "type": "1", "value": "1" } { "type": "2", "value": "2" } { "type": "3", "value": "3" } { "type": "1", "value": "4" } 

What I'm trying to do is: Write a display function:

 function (doc, meta) { emit(doc.type, 0); } 

Using the built-in reduction function:

 _count 

But I do not get the expected result.

How can I get all types?

UPDATE

Note that types are different documents, and I know that pruning works on a document and is not performed outside of it.

+4
source share
1 answer

By default, it will reduce all key groups. The function you want is called group_level :

This is equivalent to reduce=true

 ~ $ curl 'http://localhost:8092/so/_design/dev_test/_view/test?group_level=0' {"rows":[ {"key":null,"value":4} ] } 

But here is how you can get the reduction at the first key level

 ~ $ curl 'http://localhost:8092/so/_design/dev_test/_view/test?group_level=1' {"rows":[ {"key":"1","value":2}, {"key":"2","value":1}, {"key":"3","value":1} ] } 

There is also a blog post about this: http://blog.couchbase.com/understanding-grouplevel-view-queries-compound-keys

The couchbase admin pool has a corresponding option: group_level option in couchbase admin console

+8
source

Source: https://habr.com/ru/post/1499554/


All Articles