How to create relation from search result


(Cshe) #1

Hi,
i used to make relationship from a search result, in Neo4j, i can do it like this:

MATCH (sor:ASSET),(dept:DEPT) 
WHERE sor.dept = dept.name
set (sor)-[r:r_dept]->(dept) 
RETURN r

but how to do it with dgraph.
think you very much!


(Michel Conrado (Support Engineer)) #2

Hey, sorry I personally don’t know that much about n4j’s lang. Maybe other can help.

Can you explain the desired result?

It seems to me that “MATCH” would be equivalent to inequality.
https://docs.dgraph.io/query-language/#inequality and “HAS”
function https://docs.dgraph.io/query-language/#has
“WHERE” It seem to be doing a comparison of values. Maybe this Tour example fits https://tour.dgraph.io/search/4/. The “SET” I do not understand.


(Cshe) #3

Thinks Michel,

just like SQL: “insert into table_b select * from table_a”, “select * from table_a” is search result

what i want to do:
there’s 100 department node [A,B,C,D…],
and 1000 user node [user01,user02…user1000],
every user has a property named department, user01’s department is A, user02’s department is B…
i want to create relationship between every user and he’s department.

with n4j,I used to find all users and departmentes with “MATCH” and “WHERE” as result first, then create edges with “SET”.

sorry My English is not very good


(Michel Conrado (Support Engineer)) #4

In Dgraph the writing (mutation) is separated from the query. You will not be able to do something similar to SQL “insert into”. Dgraph is based on GraphQL. That is, the Dgraph (or even GraphQL) paradigm is distinct from SQL. So there’s no way to do something similar In these terms. If it’s just the query, you can do something similar.

n4j has another purpose to use Graphs, so they have the Cypher and other SQL like languages. The purpose of Dgraph or GraphQL is to keep it purely Graph and simple.

In comparison with SQL I would say something like:

select * from table_a `

{
  me(func: has(table_a)) {
    uid
    expand(_all_) {expand(_all_) {expand(_all_)} }
  }
}

In Dgraph there is no concept of Tables or external DB’s (to do Joins)

You will have to do something like Upsert Procedure
https://docs.dgraph.io/howto/#upsert-procedure

I strongly recommend you to do our tour https://tour.dgraph.io/


(Cshe) #5

I’ve probably got your idea. I’ll take a look at your suggestions first and finish reading the manual as soon as possible. Thank you again for your explanation.