Something about a query

I am new to Dgraph and I am curious that how can I query some particular nodes in it?

Let me give you a example:

With MySQL

When using MySQL, I have saved a few records which we call them Department records who save some information for different dept(s).

id dept_name some_other_fields
1 dept1 blabla
2 dept2 blabla
3 dept3 blabla

I can pick up these dept information easily with a SELECT statement.

With Dgraph

I am going to save these department as nodes(Please corrects me if I am using it wrongly):

{
  set {
    _:dept1 <name> "dept1" .
    _:dept1 <some_other_predicate> "blabla" .
   
    _:dept2 <name> "dept2" .
    _:dept2 <some_other_predicate> "blabla" .

    _:dept3 <name> "dept3" .
    _:dept3 <some_other_predicate> "blabla" .

    _:product1 <name> "product1" .
    _:product1 <belong> _:dept1 .
  }
}

And I am wondering: How can I query all the nodes from the database?

  • If I use has(name) query, I will get all the nodes including the product which doesn’t meet my requirement.
  • Of course I can use has(some_other_predicate) query to find the department just fine, but what if some other nodes save this predicate some_other_predicate in the future and it will come to the same dilemma again.

So, how can I query the department node properly and query it straight-forwardly?

What would be the best practice?

You can use type predicate to give node a type.
e.g:-

_:dept1 <name> "dept1" .
_:dept1 <some_other_predicate> "blabla" .
_:dept1 <type> "department"

and in query use eq function as eq(type,"department")
This returns all departments.

Another way is to give node specific predicate name.
e.g. -
Instead of using <name> predicate use < dept_name> and then in query use has(dept_name)
This will also returns all departments.

For more information read here - Get started with Dgraph

Read the query docs here - Get started with Dgraph
You will find all the answers there.