获取所有模型之间的关系,包括节点数据。

huangapple go评论100阅读模式
英文:

Get all the relations between models including node data

问题

我有这些节点和关系,但关于fromto节点信息有一个问题。

我有这3个交易

  • from: 111, to: 222 -> 金额: $10000
  • from: 222, to: 333 -> 金额: $2000
  • from: 222, to: 444 -> 金额: $4000

使用这个查询(不确定是否正确)

MATCH path = (:Contact {contact_id: '111'})-[t:TRANSACTION*1..3]-()
With relationships(path) as relations
unwind relations as u_relations
With distinct(u_relations) as distinct_relations
return distinct_relations

我获得了所需的信息,但我无法弄清楚如何添加每个交易的contact_id from和to,结果是

{

  "identity": 0,
  "start": 0,
  "end": 1,
  "type": "TRANSACTION",
  "properties": {
    "date": "2023-08-03T17:08:34-03:00",
    "amount": 10000.0
  },
  "elementId": "0",
  "startNodeElementId": "0",
  "endNodeElementId": "1"
}
{
  "identity": 1,
  "start": 1,
  "end": 5,
  "type": "TRANSACTION",
  "properties": {
    "date": "2023-08-03T17:08:34-03:00",
    "amount": 2000.0
  },
  "elementId": "1",
  "startNodeElementId": "1",
  "endNodeElementId": "5"
}
{
  "identity": 2,
  "start": 1,
  "end": 6,
  "type": "TRANSACTION",
  "properties": {
    "date": "2023-08-03T17:08:35-03:00",
    "amount": 4000.0
  },
  "elementId": "2",
  "startNodeElementId": "1",
  "endNodeElementId": "6"
}

如何使用startend键来获取contact_id并将其添加到结果中?

英文:

I have this nodes and relations, but I have a issue regarding the from and to node info

I have this 3 transactions

  • from: 111, to: 222 -> amount: $10000
  • from: 222, to: 333 -> amount: $2000
  • from: 222, to: 444 -> amount: $4000

With this query (not shure if it's ok)

MATCH path = (:Contact {contact_id: '111'})-[t:TRANSACTION*1..3]-()
With relationships(path) as relations
unwind relations as u_relations
With distinct(u_relations) as distinct_relations
return distinct_relations

I get the info I need, but I can't figure it out how to add the contact_id from and to of each transaction, the result is

{

  "identity": 0,
  "start": 0,
  "end": 1,
  "type": "TRANSACTION",
  "properties": {
    "date": "2023-08-03T17:08:34-03:00",
    "amount": 10000.0
  },
  "elementId": "0",
  "startNodeElementId": "0",
  "endNodeElementId": "1"
}
{
  "identity": 1,
  "start": 1,
  "end": 5,
  "type": "TRANSACTION",
  "properties": {
    "date": "2023-08-03T17:08:34-03:00",
    "amount": 2000.0
  },
  "elementId": "1",
  "startNodeElementId": "1",
  "endNodeElementId": "5"
}
{
  "identity": 2,
  "start": 1,
  "end": 6,
  "type": "TRANSACTION",
  "properties": {
    "date": "2023-08-03T17:08:35-03:00",
    "amount": 4000.0
  },
  "elementId": "2",
  "startNodeElementId": "1",
  "endNodeElementId": "6"
}

How can I use start and end keys to grab the contact_id and add it to the result?

答案1

得分: 1

开始和结束键是指Neo4j内部节点标识符,不应该在Neo4j之外使用,因为它们可能随时间而变化。

您的查询本身不应该需要使用这些键;以下查询应该有效:

MATCH (c:Contact {contact_id: '111'})-[t:TRANSACTION*1..3]-()
RETURN c, t

不需要使用distinct(t),因为Neo4j在单个路径内不会重复相同的关系。

英文:

The start and end keys are refering to the Neo4j internal node identifiers, which should not be used outside from Neo4j, as they can change over time.

Your query itself shouldn't require using those; the following query should work:

MATCH (c:Contact {contact_id: '111'})-[t:TRANSACTION*1..3]-()
RETURN c, t

Using distinct(t) is not required, as Neo4j does not the same relation twice inside a single path.

答案2

得分: 0

Here's the translated part of your text:

这可能适合你:

匹配(:{contact_id: '111' } ) - [RS:TRANSACTION*..3 ] - ()
UNWIND RS AS R
与不同
  STARTNODE(R ) 。CONTACT_ID 作为从,
  ENDNODE(R ) 。CONTACT_ID 作为TO,
  R
与从,到,收集(R.AMOUNT ) 作为金额
返回收集({从:从,到:到,金额:金额} ) 作为结果

此查询获取每个 from/to 对的所有不同关系,收集每个对的 amount 值,然后在一个单一列表中返回所有结果。

注意:如果同一对的N个关系具有相同的 amount,则该 amount 将在该对的 transactions 列表中出现N次。我认为这是你想要的。

结果应该类似于以下内容:

[
{ 
    from: '111',
    to: '222',
    amounts: [10000]
},
{ 
    from: '222',
    to: '333',
    amounts: [2000]
},
{ 
    from: '222',
    to: '444',
    amounts: [4000]
}
]
英文:

This may work for you:

MATCH (:Contact {contact_id: '111'})-[rs:TRANSACTION*..3]-()
UNWIND rs AS r
WITH DISTINCT
  STARTNODE(r).contact_id AS from,
  ENDNODE(r).contact_id AS to,
  r
WITH from, to, COLLECT(r.amount) AS amounts
RETURN COLLECT({from: from, to: to, amounts: amounts}) AS result

This query gets all distinct relationships for each from/to pair, collects the amount values for each pair, and then returns all the results in a single list.

Note: If N relationships for the same pair have the same amount, then that amount will appear N times in the transactions list for that pair. I presume this is what you wanted.

The results should look something like this:

[
{ 
    from: '111',
    to: '222',
    amounts: [10000]
},
{ 
    from: '222',
    to: '333',
    amounts: [2000]
},
{ 
    from: '222',
    to: '444',
    amounts: [4000]
}
]

huangapple
  • 本文由 发表于 2023年8月4日 05:45:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定