Querying for nodes without any friends

Using the simple data set in the dgraph tour, I’m wanting to do a query to find all the people without any friends.

I tried this:

{
  nofriends(func: eq(count(friend), 0)) {
    uid
    name@.
    age
  }
}

Which says count(predicate) cannot be used to search for negative counts (nonsensical) or zero counts (not tracked).

What would be the proper way to do this, and doing it in a way that would scale to a million nodes which is the use-case I am looking for.

Hi Arthur,
I’m newbie with Dgraph and I’m not sure if this solution is scale to a million nodes.
My schema is:

isA: string @index(hash) .
friend: uid @count .

Than I could find all persons without friend with this query:

{
  nofriends(func: eq(isA,"Person")) @filter(NOT has(friend)) {
    uid
    name@.
    age
  }
}

@artooro: We don’t support finding zero counts as the index size can explode and wouldn’t scale with millions of nodes.

@selmeci You query gets two sorted lists from index and does set difference which is efficient and scales easily. Fetching name, age for million nodes would take time even if done in parallel, so you should ideally use pagination. It would also ensure that there is no spike in memory dude to large number of results and serialisation cost.

2 Likes

@selmeci that was pretty helpful thanks. My query is now:

{
  nameswithoutfriends(func: has(name)) @filter(NOT has (friend)) {
    name@.
    age
  }
}

After adding friend: uid @count . to the schema.

Looks like dgraph is a good fit, looking forward to getting more familiar with it.

1 Like

Hi @artooro

You don’t need count index on friend to do has operation. You only need it if you want to do inequality queries at root.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.