How to calculate unique values in different groups?

Hi, I have query:

{    
    countryInfo(func: eq(user.id, ["idA", "idB", "idC"])) {
    	user.id
      countries as user.friends {
      	~country.friends {
        	uid
        }
      }
    
    	val(countries)
    }
}

Response:

{
  "data": {
    "countryInfo": [
      {
        "user.id": "idA",
        "countries": [
          {
            "~country.friends": [
              {
                "uid": "0x169a49"
              },
              {
                "uid": "0x169a48"
              }
            ]
          },
          {
            "~country.friends": [
              {
                "uid": "0x169a48"
              },
              {
                "uid": "0x169a49"
              }
            ]
          },
          {
            "~country.friends": [
              {
                "uid": "0x169a48"
              }
            ]
          }
        ]
      },
      {
        "user.id": "idB",
        "countries": [
          {
            "~country.friends": [
              {
                "uid": "0x169a4b"
              }
            ]
          }
        ]
      },
      {
        "user.id": "idC",
        "countries": [
          {
            "~country.friends": [
              {
                "uid": "0x169a4b"
              },
              {
                "uid": "0x169ba3"
              },
              {
                "uid": "0x169ba4"
              }
            ]
          }
        ]
      }
    ]
  }
}

But I only need to calculate the unique countries for each “user.id”, how can I do that?

I need response

{
  "data": {
    "countryInfo": [
      {
        "user.id": "idA",
        "uniqueCountries": 2
      },
      {
        "user.id": "idB",
        "uniqueCountries": 1
      },
      {
        "user.id": "idC",
        "uniqueCountries": 3
          }
        ]
      }
    ]
  }
}

Thank you

Check out this documentation:

https://dgraph.io/docs/query-language/count/

Although, I don’t think there is a way to count distinct of a grandchild relationship. :confused: Yet another missing feature. There is no loop mechanism either in DQL to make this happen. You will probably have to query all of the data and then post-process it somewhere to get the counts you want on a client/middleman-server.