简体   繁体   中英

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:

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.

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.

Nodes in neo4j are maps (property name keys); 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? Creation order? Last update order? Numerically by ID? I think the answer is "never assume, use ORDER BY ".

If you need the order to be consistent, use 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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