处理 apoc.merge.node() Neo4j 中的空值
Hanlde nulls inside apoc.merge.node() Neo4j
我正在将 Neo4j 与节点 js 一起使用。在节点 js 中,我得到了一组对象,每个对象都是一个节点,所以对象看起来像这样。
[{
id:'uuid string',
name:'node name',
type:'Author',
createdAt:'some Date',
someOtherProperties:'their values',
}]
为了创建节点,我使用 apoc.merge.node()
函数。
UNWIND $nodes AS newNode
CALL apoc.merge.node([newNode.type], {id :newNode.id}, newNode, newNode)
YIELD node
RETURN node
有时我会得到对象的一个属性为空。有没有办法在 cypher 中处理 null,因为我不想在节点 js 中迭代 throw 数组?有时我的数组可能很长,所以我不想通过迭代来减慢它的速度。
仅当 type 或 id 为空时,您的查询才会失败,其他属性可以为空。如果 type 或 id 属性为空,您可以定义默认值。
UNWIND $nodes AS newNode
CALL apoc.merge.node([coalesce(newNode.type, 'Default')],
{id :coalesce(newNode.id, apoc.create.uuid())}, apoc.map.clean(newNode, ['type', 'id'], []), apoc.map.clean(newNode, ['type','id'],[]))
YIELD node
RETURN node
我添加了节点标签的默认值:默认,我还添加了使用 apoc.create.uuid
生成的默认 uuid。此外,最佳做法是不要更新用作标识属性的属性,因此我添加了 apoc.map.clean
以删除要更新的 id 和 type 属性。
根据附加问题进行编辑:
Is there a way to pass the node without creating if the id is null ? –
UNWIND $nodes AS newNode
WITH newNode
WHERE newNode.id IS NOT NULL
CALL apoc.merge.node([coalesce(newNode.type, 'Default')],
{id :newNode.id}, apoc.map.clean(newNode, ['type', 'id'], []), apoc.map.clean(newNode, ['type','id'],[]))
YIELD node
RETURN distinct 'done'
我正在将 Neo4j 与节点 js 一起使用。在节点 js 中,我得到了一组对象,每个对象都是一个节点,所以对象看起来像这样。
[{
id:'uuid string',
name:'node name',
type:'Author',
createdAt:'some Date',
someOtherProperties:'their values',
}]
为了创建节点,我使用 apoc.merge.node()
函数。
UNWIND $nodes AS newNode
CALL apoc.merge.node([newNode.type], {id :newNode.id}, newNode, newNode)
YIELD node
RETURN node
有时我会得到对象的一个属性为空。有没有办法在 cypher 中处理 null,因为我不想在节点 js 中迭代 throw 数组?有时我的数组可能很长,所以我不想通过迭代来减慢它的速度。
仅当 type 或 id 为空时,您的查询才会失败,其他属性可以为空。如果 type 或 id 属性为空,您可以定义默认值。
UNWIND $nodes AS newNode
CALL apoc.merge.node([coalesce(newNode.type, 'Default')],
{id :coalesce(newNode.id, apoc.create.uuid())}, apoc.map.clean(newNode, ['type', 'id'], []), apoc.map.clean(newNode, ['type','id'],[]))
YIELD node
RETURN node
我添加了节点标签的默认值:默认,我还添加了使用 apoc.create.uuid
生成的默认 uuid。此外,最佳做法是不要更新用作标识属性的属性,因此我添加了 apoc.map.clean
以删除要更新的 id 和 type 属性。
根据附加问题进行编辑:
Is there a way to pass the node without creating if the id is null ? –
UNWIND $nodes AS newNode
WITH newNode
WHERE newNode.id IS NOT NULL
CALL apoc.merge.node([coalesce(newNode.type, 'Default')],
{id :newNode.id}, apoc.map.clean(newNode, ['type', 'id'], []), apoc.map.clean(newNode, ['type','id'],[]))
YIELD node
RETURN distinct 'done'