简体   繁体   English

为什么neo4j查询会不按顺序返回东西,我该如何按顺序排列东西?

[英]Why does a neo4j query return things out of order and how do I put things in order?

I expected a Neo4J query to return data listed in the same order for each entry (data 1 being first, data 2 second, or atleast a consistent order.)I executed a query to return a bunch of nodes of the same type and this came out: 我希望Neo4J查询返回的数据以相同的顺序列出每个条目(数据1是第一个,数据2是第二个,或者至少是一个一致的顺序)。我执行了一个查询以返回一堆相同类型的节点,结果出:

Node1: Data1, Data2 节点1:数据1,数据2

Node2: Data1, Data2 节点2:数据1,数据2

Node3: Data2, Data1 节点3:数据2,数据1

Node 4: Data2, Data1 节点4:Data2,Data1

and so on and so forth. 等等等等。 Why are the properties in a random order and how can I fix this? 为什么属性按随机顺序排列,我该如何解决?

Edit: 编辑:

I used this to create the nodes: 我用它来创建节点:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM
'filepath' AS row
CREATE (:Node {Data1: row.Data1, Data2: row.Data2})

I then used this query: 然后,我使用了以下查询:

MATCH (n:Node) RETURN n LIMIT 25

It returns 18 rows which is right, but some of the nodes list the properties in a different order. 它返回正确的18行,但是某些节点以不同的顺序列出了属性。

I might be wrong, but lacking an ORDER BY clause in a query, no query language I've ever heard of (cypher included) guarantees any particular ordering. 我可能是错的,但是查询中缺少ORDER BY子句,我听说过的任何查询语言(包括密码)都不能保证任何特定的排序。

Nodes in neo4j are maps (property name keys); neo4j中的节点是映射(属性名称键); in general, maps/hashtables don't guarantee ordering on keys unless you use particular types of maps. 通常,除非您使用特定类型的地图,否则地图/哈希表不保证键的顺序。

I think for most data query languages, it's not a good idea to ever rely on implicit ordering; 我认为对于大多数数据查询语言来说,依靠隐式排序并不是一个好主意。 for example, if you just do MATCH n RETURN n what order will the n nodes come back in? 例如,如果您只执行MATCH n RETURN n ,则n节点将以什么顺序返回? Creation order? 创建顺序? Last update order? 最后更新顺序? Numerically by ID? 通过数字编号? I think the answer is "never assume, use ORDER BY ". 我认为答案是“永远不要假设,请使用ORDER BY ”。

If you need the order to be consistent, use ORDER BY . 如果需要顺序一致,请使用ORDER BY

You can do something like this: 您可以执行以下操作:

MATCH n 
WITH id(n) as idn, keys(n) as kz unwind kz as items 
RETURN idn, items 
ORDER BY idn, items;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM