# Count all nodes in Dgraph

**URL:** <https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261>\
**Category:** GraphQL\
**Tags:** dgraph\
**Created:** [August 6, 2020, 7:02am UTC](https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261 "2020-08-06T07:02:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![slawo](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/slawo/32/4279_2.png) [@slawo](https://discuss.dgraph.io/u/slawo)\
**Post date:** [August 6, 2020, 7:02am UTC](https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261/1 "2020-08-06T07:02:40Z")

</div>

Is there a way of retrieving the count of all existing nodes in the graph?  
I don’t need todo this often, only for sanity checks. there used to be a way using `has(_predicate_)` but it doesn’t work anymore.

---

<div class="post-metadata">

**Author:** ![abhimanyusinghgaur](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/abhimanyusinghgaur/32/2980_2.png) [@abhimanyusinghgaur](https://discuss.dgraph.io/u/abhimanyusinghgaur)\
**Post date:** [August 6, 2020, 7:05am UTC](https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261/2 "2020-08-06T07:05:50Z")

</div>

If all nodes in your db have the `dgraph.type` edge, then you can do this:

```auto
query {
  count(func: has(dgraph.type)) {
    count(uid)
  }
}

```

---

<div class="post-metadata">

**Author:** ![slawo](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/slawo/32/4279_2.png) [@slawo](https://discuss.dgraph.io/u/slawo)\
**Post date:** [August 6, 2020, 11:39am UTC](https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261/3 "2020-08-06T11:39:32Z")

</div>

It seems none of the nodes have been created with that edge

---

<div class="post-metadata">

**Author:** ![Naman](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/naman/32/3498_2.png) [@Naman](https://discuss.dgraph.io/u/Naman)\
**Post date:** [August 6, 2020, 12:22pm UTC](https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261/4 "2020-08-06T12:22:52Z")

</div>

Hi @slawo, you need to have defined `dgraph.type` to the nodes. For example:

```auto
{
  set {
    _:a <name> "Becky" .
    _:a <dgraph.type> "Pet" .
  }
}

```

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [August 6, 2020, 12:44pm UTC](https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261/5 "2020-08-06T12:44:01Z")

</div>

Welcome Slawo! if you don’t have types (nodes with dgraph.type) and need a reliable sanity check of whats in the db, you could [export](https://dgraph.io/docs/deploy/dgraph-administration/#exporting-database) data from alpha regularly. The data is exported in rdf or json format and you can get counts from there. I know it sounds like an overkill, but it is an option that will serve the need fully.

---

<div class="post-metadata">

**Author:** ![abhimanyusinghgaur](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/abhimanyusinghgaur/32/2980_2.png) [@abhimanyusinghgaur](https://discuss.dgraph.io/u/abhimanyusinghgaur)\
**Post date:** [August 6, 2020, 1:32pm UTC](https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261/6 "2020-08-06T13:32:38Z")

</div>

You could use the `/state` endpoint to get `maxLeaseId`. That will give you an upper bound on the count of uids leased till now, but not the exact count. There is no other easy way than to have that `dgraph.type` edge.

---

<div class="post-metadata">

**Author:** ![EnricoMi](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@EnricoMi](https://discuss.dgraph.io/u/EnricoMi)\
**Post date:** [August 6, 2020, 4:39pm UTC](https://discuss.dgraph.io/t/count-all-nodes-in-dgraph/9261/7 "2020-08-06T16:39:14Z")

</div>

If your schema has only a few predicates, but no / not all nodes have `dgraph.type` assigned, then you can do the following (here there are only three predicates in the schema:

```auto
pred1 as var(func: has(<dgraph.graphql.schema>))
pred2 as var(func: has(<dgraph.graphql.xid>))
pred3 as var(func: has(<dgraph.type>))

result (func: uid(pred1,pred2,pred3)) {
  count(uid)
}

```

You can retrieve the schema programaticaly and then generate this query. It works nicely with hundreds of predicates.
