回调中的唯一计数
Unique counts in a callback
我有一个用户列表,我正在尝试获取用户电子邮件地址中每个唯一域的结果及其总数。
所以,假设我有这 5 个用户:
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| id | email | firstname | lastname | something |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00c0f0db-87d0-45b2-8ed2-aa94d1e3e659 | shane.conte@jourrapide.com | Shane | Conte | iew9anap0L |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 0114360a-3ef8-49d6-8c51-02392bc51e10 | michelle.guitierrez@dayrep.com | Michelle | Guitierrez | eeNgiev3foh |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00e8e2f2-2130-4f65-8914-b93d5b029d75 | terri.hebert@rhyta.com | Terri | Hebert | vahMoKiuCh0 |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00e1578b-cf6d-46b8-92e3-2388a80105f7 | richard.copeland@dayrep.com | Richard | Copeland | Iem4mohng |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00f1be34-d60e-4b2f-b3ae-610c67151f2d | elsie.fuhrman@rhyta.com | Elsie | Fuhrman | aPie6piD6ae |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
运行完成查询后,我希望看到这个结果:
+-------+----------------+
| count | domain |
+-------+----------------+
| 1 | jourrapide.com |
+-------+----------------+
| 2 | dayrep.com |
+-------+----------------+
| 2 | rhyta.com |
+-------+----------------+
我目前正在 运行 下面查询以获取唯一域,但是如果我尝试在其中 运行 count()
,它会在 300 秒后显着失败,我预计会发生,因为我有超过 5 个用户:)
r.db('helloworld').table('users').pluck('email').map(function(user) {
return user('email').split('@').nth(1).downcase()
}).distinct().map(function(domain) {
return {
count: '???', // <--- this is where I need help
domain: domain
}
})
正如你想象的那样,它完美地 returns 这个结果:
+-------+----------------+
| count | domain |
+-------+----------------+
| ??? | jourrapide.com |
+-------+----------------+
| ??? | dayrep.com |
+-------+----------------+
| ??? | rhyta.com |
+-------+----------------+
我希望这是有道理的。如果您认为我走错了路,请随时提出任何其他建议。
提前致谢!
你不能 distinct()
到 count()
。相反,你想要 group()
:
r.db('helloworld').table('users').pluck('email').map(function(user) {
// wrap the result in an object for grouping purposes
return { domain: user('email').split('@').nth(1).downcase() };
})
// this groups all domains together in a [{ group, reduction }] list of objects
.group('domain')
// after group(), calls are scoped to each reduction: count each one
.count()
// let's ungroup to scope the following calls to the whole sequence
.ungroup()
// let's be compliant with the format you expect
.map(function(doc) {
return {
domain: doc('group'),
count: doc('reduction')
};
});
我有一个用户列表,我正在尝试获取用户电子邮件地址中每个唯一域的结果及其总数。
所以,假设我有这 5 个用户:
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| id | email | firstname | lastname | something |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00c0f0db-87d0-45b2-8ed2-aa94d1e3e659 | shane.conte@jourrapide.com | Shane | Conte | iew9anap0L |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 0114360a-3ef8-49d6-8c51-02392bc51e10 | michelle.guitierrez@dayrep.com | Michelle | Guitierrez | eeNgiev3foh |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00e8e2f2-2130-4f65-8914-b93d5b029d75 | terri.hebert@rhyta.com | Terri | Hebert | vahMoKiuCh0 |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00e1578b-cf6d-46b8-92e3-2388a80105f7 | richard.copeland@dayrep.com | Richard | Copeland | Iem4mohng |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00f1be34-d60e-4b2f-b3ae-610c67151f2d | elsie.fuhrman@rhyta.com | Elsie | Fuhrman | aPie6piD6ae |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
运行完成查询后,我希望看到这个结果:
+-------+----------------+
| count | domain |
+-------+----------------+
| 1 | jourrapide.com |
+-------+----------------+
| 2 | dayrep.com |
+-------+----------------+
| 2 | rhyta.com |
+-------+----------------+
我目前正在 运行 下面查询以获取唯一域,但是如果我尝试在其中 运行 count()
,它会在 300 秒后显着失败,我预计会发生,因为我有超过 5 个用户:)
r.db('helloworld').table('users').pluck('email').map(function(user) {
return user('email').split('@').nth(1).downcase()
}).distinct().map(function(domain) {
return {
count: '???', // <--- this is where I need help
domain: domain
}
})
正如你想象的那样,它完美地 returns 这个结果:
+-------+----------------+
| count | domain |
+-------+----------------+
| ??? | jourrapide.com |
+-------+----------------+
| ??? | dayrep.com |
+-------+----------------+
| ??? | rhyta.com |
+-------+----------------+
我希望这是有道理的。如果您认为我走错了路,请随时提出任何其他建议。
提前致谢!
你不能 distinct()
到 count()
。相反,你想要 group()
:
r.db('helloworld').table('users').pluck('email').map(function(user) {
// wrap the result in an object for grouping purposes
return { domain: user('email').split('@').nth(1).downcase() };
})
// this groups all domains together in a [{ group, reduction }] list of objects
.group('domain')
// after group(), calls are scoped to each reduction: count each one
.count()
// let's ungroup to scope the following calls to the whole sequence
.ungroup()
// let's be compliant with the format you expect
.map(function(doc) {
return {
domain: doc('group'),
count: doc('reduction')
};
});