rethinkdb值的频率

在reThinkDB中为某些值实现“频率”查询的方式是什么?

示例数据:

[{ akey : "1", anotherkey : "2" }, { akey : "1", anotherkey : "2" }, { akey : "AAA", anotherkey : "BBB" }, { akey : "CCC", anotherkey : "CCC" }] 

应该作为参数输出:

 { "1" : 2, "AAA" : 1, "CCC" : 1 } 

应该anotherkey作为参数:

 { "2" : 2, "BBB" : 1, "CCC" : 1 } 

你可以尝试这样的事情:

 r.expr([{ akey : "1", anotherkey : "2" }, { akey : "1", anotherkey : "2" }, { akey : "AAA", anotherkey : "BBB" }, { akey : "CCC", anotherkey : "CCC" }]).map(function(doc) { return r.branch(doc.keys().contains('akey'),{'value': doc('akey')},{}) }) .group('value') .count() .ungroup() .map(function(doc) { return r.object(doc('group'), doc('reduction')) }) .reduce(function(left, right) { return left.merge(right) }) 

另一种不reduce是:

 r.expr([{ akey : "1", anotherkey : "2" }, { akey : "1", anotherkey : "2" }, { akey : "AAA", anotherkey : "BBB" }, { akey : "CCC", anotherkey : "CCC" }]).map(function(doc) { return r.branch(doc.keys().contains('anotherkey'),{'value': doc('anotherkey')},{}) }) .group('value') .count() .ungroup() .map(function(doc) { return [doc('group'), doc('reduction')] }) .coerceTo('object') 

但是,我喜欢reduce方式,因为它映射到我如何思考整个过程。