chaeyk
chaeyk

Reputation: 591

Couchbase map/reduce apply earlier group or key selection?

Document says that reduce/group/group_level is applied, and later key selection is applied. But my test results in opposite.

Documents

{ "k1": 1, "k2": 1 }
{ "k1": 1, "k2": 2 }

map

emit([doc.k1, doc.k2], null)

reduce

_count

test

query.group_level(1).key(1) = 0         // expected 2
query.group_level(1).key([1,1]) = 1     // expected 0

My test was wrong?

Upvotes: 0

Views: 43

Answers (1)

Cecile
Cecile

Reputation: 26

your test result seems to be quite right : key selection is applied before the reduce.

query.group_level(1) means you want to count every row in the views, group by the first key.

If you execute this query, you got a single result : [1] => 2

If you add .key(xxx), this means that before the reduce, you want to filter the data set of the views to only the key xxx.

if xxx=1, there is no result because there is no row in the view with the key 1.

if xxx=[1,1] there is a single result because there is a single row with the key [1,1]

I opened a ticket to update the documentation in here : https://issues.couchbase.com/browse/DOC-1029

Upvotes: 1

Related Questions