collect函数

collect()函数返回一个符合表达式返回结果的列表。该函数可以将多条记录或值合并进一个列表,实现数据聚合。

collect()是一个聚合函数,类似SQL中的GROUP BY

示例

  1. nebula> UNWIND [1, 2, 1] AS a \
  2. RETURN a;
  3. +---+
  4. | a |
  5. +---+
  6. | 1 |
  7. +---+
  8. | 2 |
  9. +---+
  10. | 1 |
  11. +---+
  12. nebula> UNWIND [1, 2, 1] AS a \
  13. RETURN collect(a);
  14. +------------+
  15. | collect(a) |
  16. +------------+
  17. | [1, 2, 1] |
  18. +------------+
  19. nebula> UNWIND [1, 2, 1] AS a \
  20. RETURN a, collect(a), size(collect(a));
  21. +---+------------+------------------+
  22. | a | collect(a) | size(COLLECT(a)) |
  23. +---+------------+------------------+
  24. | 2 | [2] | 1 |
  25. +---+------------+------------------+
  26. | 1 | [1, 1] | 2 |
  27. +---+------------+------------------+
  28. # 降序排列,限制输出行数为3,然后将结果输出到列表中。
  29. nebula> UNWIND ["c", "b", "a", "d" ] AS p \
  30. WITH p AS q \
  31. ORDER BY q DESC LIMIT 3 \
  32. RETURN collect(q);
  33. +-----------------+
  34. | collect(q) |
  35. +-----------------+
  36. | ["d", "c", "b"] |
  37. +-----------------+
  38. nebula> WITH [1, 1, 2, 2] AS coll \
  39. UNWIND coll AS x \
  40. WITH DISTINCT x \
  41. RETURN collect(x) AS ss;
  42. +--------+
  43. | ss |
  44. +--------+
  45. | [1, 2] |
  46. +--------+
  47. nebula> MATCH (n:player) \
  48. RETURN collect(n.age);
  49. +---------------------------------------------------------------+
  50. | collect(n.age) |
  51. ----------------------------------------------------------------+
  52. | [32, 32, 34, 29, 41, 40, 33, 25, 40, 37, ...
  53. ...
  54. # 基于年龄聚合姓名。
  55. nebula> MATCH (n:player) \
  56. RETURN n.age AS age, collect(n.name);
  57. +-----+--------------------------------------------------------------------------+
  58. | age | collect(n.name) |
  59. +-----+--------------------------------------------------------------------------+
  60. | 24 | ["Giannis Antetokounmpo"] |
  61. +-----+--------------------------------------------------------------------------+
  62. | 20 | ["Luka Doncic"] |
  63. +-----+--------------------------------------------------------------------------+
  64. | 25 | ["Joel Embiid", "Kyle Anderson"] |
  65. +-----+--------------------------------------------------------------------------+
  66. ...

最后更新: June 23, 2021