是否可以确定Cypher MERGE是创建还是匹配? [英] Is it possible to determine if a Cypher MERGE results in a create or a match?

查看:99
本文介绍了是否可以确定Cypher MERGE是创建还是匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑Neo4J 2.0 Cypher查询

Consider the Neo4J 2.0 Cypher query

MERGE (u:User {id_str:"123"}) 
  ON CREATE SET
    u.name="Bob",
  ON MATCH SET
    u.phone_num="555-4152"
RETURN u

这是一个愚蠢的查询-不必担心这里的意图。问题是,如何理解此查询是创建还是仅找到一个节点?

It's a silly query - don't worry about the intent here. The question is, how do I understand whether this query created or simply found a node?

UPDATE

也许我应该进一步激发为什么我想要这种行为。原因如下:如果该节点已经存在,则不必转到远程服务器(在我的情况下为Twitter API)并下载用户的所有元数据。如果ON CREATE可以以某种方式链接回回调以拉出此数据,那将是很好的。这种行为在Cypher中似乎不太可能实现。因此,大概我想做的是一个匹配,如果获取返回为NULL,那么我将调出Twitter API,获取元数据并进行创建。

Perhaps I should further motivate why I want this behavior. Here's why: If the node already exists, then don't have to go to a remote server (Twitter API in my case) and download all of the user's metadata. It would be nice if the ON CREATE could somehow link back out to a callback to pull down this data. It seems unlikely that this behavior is achievable in Cypher. So probably what I'll want to do is do a match, and if the get comes back NULL, then I'll call out to the Twitter API, get the metadata and do a create.

推荐答案

我认为这与 MERGE 的意图相反。您将使用它来描述查询执行后想要图形的外观,而不是查找图形之前的样子–读写操作在Cypher中应该严格分开。也就是说,您可以设置诸如@LameCoder所述的虚拟属性:

I think that's contrary to the intention of MERGE. You would use it to describe what you want the graph to look like after your query has executed, not to find out what it looks like before–read and write operations are supposed to be kept strictly apart in Cypher. That said, you can set a dummy property like @LameCoder mentioned:

MERGE (u:User {id_str:"123"}) 
  ON CREATE SET u.onCreate = true
WITH u, exists(u.onCreate) as onCreate
REMOVE u.onCreate
RETURN u, onCreate

将返回(u),如果创建,则返回true (u),否则为false ,并且没有任何副作用。或者,您可以维护时间戳,也许是一个用于创建节点的时间戳,一个是关于上次修改节点的时间戳–我认为这是文档中的一个示例。

will return (u), true if created, (u), false if not, and leave no side effects. Or you can maintain timestamps, perhaps one for node creation and one for when the node was last modified–I think that's an example from the documentation. This

MERGE (u:User {id_str:"123"}) 
  ON CREATE SET u.created = timestamp()
  ON MATCH SET u.lastModified = timestamp()
RETURN u, has(u.lastModified) as onMerge

与上面的查询类似。

这篇关于是否可以确定Cypher MERGE是创建还是匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆