How to aggregate documents

Hi everybody!

I have documents that contains 1 minute electric motor data:

id = motor_<motor name>_<timestamp>

{
  "type": "motor",
  "timestamp": "xxx",
  "rotation": {
       "values": [ v1, v2, ..., vn]
  }
  ...
  otherProperties: ...,
  ...
}

I need to aggregate documents by a particular propertie as following:

{
  "rotation": {
       "values": [ v1, v2, ..., vn, v11, v12, ..., v1n, v21, v22, ..., v2n]
  }
}

In other words… I need a data segment greater then 1 minute, ie, an union of documents.
Is it possible with a view (map reduce)?

It is probably possible with map reduce.

If you use Couchbase 4.x, you can also use a N1QL query:

SELECT rotation, ARRAY_AGG(`values`) AS values
FROM my_bucket
GROUP BY rotation;

Which type has best performance for read data segments? MapReduce or N1QL?
I need to read millions of segments.

I thought 2 steps. My map works very well. It returns for each document:

{
  "rotation": {
       "values": [ v1, v2, ..., vn]
  }
}

But… I can’t write a reduce script that works. Can you help me?

Someone else will help you with the reduce script.

As for performance, it depends on whether you read the same data many times. N1QL results are computed every time, while map-reduce results are computed incrementally and saved.