Neo4j属性顺序可以控制吗?

3

有没有控制属性顺序的机制?

我无法在http://www.neo4j.org/console上重现这个问题。

如果我使用Neo4j 1.9.2 Community,执行以下操作:

CREATE (m1 {`$type`: {moduleTypeName}, Name: 'M1', ModelNumber: 'MN1'})

稍后,当我从REST Cypher端点使用Cypher查询获取此节点时,我会收到以下内容...
{
    "extensions": {},
    "paged_traverse": "http://localhost:7575/db/data/node/3777/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "outgoing_relationships": "http://localhost:7575/db/data/node/3777/relationships/out",
    "traverse": "http://localhost:7575/db/data/node/3777/traverse/{returnType}",
    "all_typed_relationships": "http://localhost:7575/db/data/node/3777/relationships/all/{-list|&|types}",
    "property": "http://localhost:7575/db/data/node/3777/properties/{key}",
    "all_relationships": "http://localhost:7575/db/data/node/3777/relationships/all",
    "self": "http://localhost:7575/db/data/node/3777",
    "properties": "http://localhost:7575/db/data/node/3777/properties",
    "outgoing_typed_relationships": "http://localhost:7575/db/data/node/3777/relationships/out/{-list|&|types}",
    "incoming_relationships": "http://localhost:7575/db/data/node/3777/relationships/in",
    "incoming_typed_relationships": "http://localhost:7575/db/data/node/3777/relationships/in/{-list|&|types}",
    "create_relationship": "http://localhost:7575/db/data/node/3777/relationships",
    "data": {
        "ModelNumber": "MN1",
        "$type": "ModuleType",
        "Name": "M1"
    }
}

我正在使用http://james.newtonking.com/pages/json-net.aspx来解析JSON,为了让它自动推断对象类型,$type属性必须排在首位。当您在流中解析JSON时,这是有意义的,因为您不想先将整个JSON加载到内存中。
似乎它并不按字母顺序排列,也不似乎是随机的。对于不同的对象类型,它的顺序是一致的,但在它们之间则不一致。
我还在Shell中提取了节点,因此似乎顺序不取决于我获取节点的方式,但也与我创建节点的顺序无关。
2个回答

3

属性没有保证的顺序。不要对“可能”的排序做任何假设。即将推出的版本可能会更改此假定行为并破坏您的代码。

我想在Cypher中,不返回节点本身而是返回属性列表会更简单,例如:

START node=node(<myid>)
RETURN node.`$type`, node.ModelNumber, node.Name

这个有定义的列。


不幸的是,密码返回的属性以 {columns:[], data:[]} 的方式排序,这使得它对于 POJO/POCO 映射有点疯狂。 - LameCoder
如果你只使用JSON响应的数据部分,那么就没问题了。 - Stefan Armbruster

1

明显地,它似乎没有那个功能。我的解决方法是用前缀aXX_来别名属性,例如a01_、a02_、a03_,然后在代码中将其删除。虽然不太美观,但它能够正常工作,因为neo4j尊重数字顺序。但是它需要以字母字符开头,因此在数字前面加上"a"。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接