collect函数

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

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

示例

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

最后更新: October 27, 2021