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

时间:2015-07-28 16:30:55

标签: database neo4j cypher graph-databases

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:

Node1: Data1, Data2

Node2: Data1, Data2

Node3: Data2, Data1

Node 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.

1 个答案:

答案 0 :(得分:2)

我可能错了,但在查询中缺少ORDER BY子句,没有我听说过的查询语言(包括cypher)保证任何特定的排序。

neo4j中的节点是地图(属性名称键);通常,map / hashtables不保证按键排序,除非您使用特定类型的地图。

我认为对于大多数数据查询语言来说,依靠隐式排序并不是一个好主意;例如,如果您只是执行MATCH n RETURN n n个节点将返回的顺序是什么?创作顺序?最后更新订单? ID数字?我认为答案是“永远不要假设,使用ORDER BY”。

如果您需要订单一致,请使用ORDER BY

您可以这样做:

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