rethinkdb 值的频率
rethinkdb frequency of values
在 reThinkDB 中实现某个值的 "frequency" 查询的方法是什么?
示例数据:
[{
akey : "1",
anotherkey : "2"
}, {
akey : "1",
anotherkey : "2"
}, {
akey : "AAA",
anotherkey : "BBB"
}, {
akey : "CCC",
anotherkey : "CCC"
}]
应该让 akey
作为参数:
{
"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
方式,因为它反映了我对整个过程的看法。
在 reThinkDB 中实现某个值的 "frequency" 查询的方法是什么?
示例数据:
[{
akey : "1",
anotherkey : "2"
}, {
akey : "1",
anotherkey : "2"
}, {
akey : "AAA",
anotherkey : "BBB"
}, {
akey : "CCC",
anotherkey : "CCC"
}]
应该让 akey
作为参数:
{
"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
方式,因为它反映了我对整个过程的看法。