Уникальное количество в обратном вызове - PullRequest
0 голосов
/ 07 октября 2019

У меня есть список пользователей, и я пытаюсь получить результат каждого уникального домена в адресах электронной почты пользователей и их итогах.

Итак, скажем, у меня есть 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
  }
})

И, как вы можете себе представить, он отлично возвращает этот результат:

+-------+----------------+
| count | domain         |
+-------+----------------+
|  ???  | jourrapide.com |
+-------+----------------+
|  ???  | dayrep.com     |
+-------+----------------+
|  ???  | rhyta.com      |
+-------+----------------+

Надеюсь, это имеет смысл. Если вы думаете, что я на неправильном пути, не стесняйтесь предложить любой другой путь.

Заранее спасибо!

1 Ответ

1 голос
/ 08 октября 2019

Вы не можете 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')
  };
});
...