dgraph 创建节点和边的时候判断节点是否存在,如果存在,就不创建,如果不存在,则创建

网友投稿 314 2022-08-25

dgraph 创建节点和边的时候判断节点是否存在,如果存在,就不创建,如果不存在,则创建

neo4j里面有一个merge的操作,能够自动帮助我们来判断节点和边在数据库里面属否存在,dgraph有没有呢,答案是有的,就是upsert,我的示例代码:

upsert { query { var(func: eq(person_id, "xxx")) { Person as uid } var(func: eq(company_id, "yyy")) { Company as uid } } mutation { set { uid(Person) "罗志祥" . uid(Person) "xxx" . uid(Person) uid(Company) (position="CEO") . uid(Person) "Person" . uid(Company) "无名公司" . uid(Company) "Company" . uid(Company) "yyy" . } } }

来看看官网怎么解释的:

To use ​​upsert operations​​​ on a predicate, specify the ​​@upsert​​​ directive in the schema. When committing transactions involving predicates with the ​​@upsert​​ directive, Dgraph checks index keys for conflicts, helping to enforce uniqueness constraints when running concurrent upserts.

我的schema为:

: string @index(exact) @upsert . : string @index(term) . : string @index(hash) @upsert .

是不是很简单呢。

怎么验证呢,多moute几次,然后用下面的语句查一下,看有没有重复:

{ data(func: eq(person_id, "xxx")) { name person_id manager @facets(orderasc:position) { name company_id } }}

参考文献

[1]. ​​IDs and Upsert Block. ​​https://dgraph.io/docs/mutations/#external-ids-and-upsert-block​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:首席营销官:6万人砍价不成功?拼多多公关塌了!(拼多多砍一刀的营销手段)
下一篇:python requests post和get请求,并把结果写入json文件中
相关文章

 发表评论

暂时没有评论,来抢沙发吧~