To count products and groupby

I want to get how many of each product was bought

I am trying with this query:

{
  var(func: has(products)) @groupby(products) {
  	a as count(uid)
  }
  
  q(func: uid(a), orderdesc: val(a)) {
		idProduct
    productName
    total_product : val(a)
  }
}

Buy I am receiving the products without grouping

{
  "data": {
    "q": [
      {
        "idProduct": "2a80be10",
        "productName": "La terra fina",
        "total_product": 1
      },
      {
        "idProduct": "bcd8f4a8",
        "productName": "Rice A Roni Instant Chicken Flavor 1.97 Ounce Cup",
        "total_product": 1
      },

My schema is this:

op.Schema = `
		id: string@index(exact) .
		name: string .
		age: int .
		purchases: [uid]@count@reverse .
		idPurchase: string .
		idPerson: string .
		ip: string@index(exact) .
		device: string .
		products: [uid] .
		idProduct: string .
		productName: string .
		price: string . 
	`
<age>: int .
<device>: string .
<dgraph.graphql.schema>: string .
<id>: string @index(exact) .
<idPerson>: string .
<idProduct>: string .
<idPurchase>: string .
<ip>: string @index(exact) .
<name>: string .
<price>: string .
<productName>: string .
<products>: [uid] .
<purchases>: [uid] @count @reverse .
type <dgraph.graphql> {
	dgraph.graphql.schema
}

Thank you in advance for any idea.

Can you share the entities type?

Well, I didn’t know how to write them. But I have the structs of golang.

type Customer struct {
	Uid       string     `json:"uid,omitempty"`
	Id        string     `json:"id,omitempty"`
	Name      string     `json:"name,omitempty"`
	Age       int        `json:"age,omitempty"`
	Purchases []Purchase `json:"purchases,omitempty"`
}

type Purchase struct {
	Uid        string    `json:"uid,omitempty"`
	IdPurchase string    `json:"idPurchase,omitempty"`
	IdPerson   string    `json:"idPerson,omitempty"`
	Ip         string    `json:"ip,omitempty"`
	Device     string    `json:"device,omitempty"`
	Products   []Product `json:"products,omitempty"`
}

type Product struct {
	IdProduct   string `json:"idProduct,omitempty"`
	ProductName string `json:"productName,omitempty"`
	Price       string `json:"price,omitempty"`
}

How can I code those entities?

Do you have those types in the DQL schema?

Something like

type Customer {
    idPerson
    name
    age
    products
}

No, I don’t. How can I add those to my schema?

For me it returns what you have asked.

{
  "data": {
    "q": [
      {
        "idProduct": "P1",
        "productName": "",
        "total_product": 3
      },
      {
        "idProduct": "P2",
        "productName": "",
        "total_product": 2
      },
      {
        "idProduct": "P3",
        "productName": "",
        "total_product": 2
      }
    ]
  }
}

Sample dataset used.

{
   "set": [
      {
         "id": "User1",
         "name": "Lucas",
         "age": "21",
         "dgraph.type": "Customer",
         "purchases": [
            {
               "idPurchase": "F32VF",
               "ip": "192.177.55.45",
               "device": "macOS",
               "dgraph.type": "Purchase",
               "products": [
                  {
                     "uid": "_:P1",
                     "idProduct": "P1",
                     "productName": "",
                     "price": "900",
                     "dgraph.type": "Product"
                  },
                  {
                     "uid": "_:P2",
                     "idProduct": "P2",
                     "productName": "",
                     "price": "765",
                     "dgraph.type": "Product"
                  },
                  {
                     "uid": "_:P3",
                     "idProduct": "P3",
                     "productName": "",
                     "price": "80",
                     "dgraph.type": "Product"
                  }
               ]
            }
         ]
      },
      {
         "id": "User2",
         "name": "Julios",
         "age": "51",
         "dgraph.type": "Customer",
         "purchases": [
            {
               "idPurchase": "F85BT",
               "ip": "192.177.2.99",
               "device": "Android",
               "dgraph.type": "Purchase",
               "products": [
                  {
                     "uid": "_:P1"
                  },
                  {
                     "uid": "_:P2"
                  },
                  {
                     "uid": "_:P3"
                  }
               ]
            }
         ]
      },
      {
         "id": "User3",
         "name": "Ivana",
         "age": "27",
         "dgraph.type": "Customer",
         "purchases": [
            {
               "idPurchase": "F012CX",
               "ip": "192.177.33.1",
               "device": "Windows 10",
               "dgraph.type": "Purchase",
               "products": [
                  {
                     "uid": "_:P1"
                  }
               ]
            }
         ]
      }
   ]
}

The correct Schema

type customer  {
	id
	name
	age
	purchases
}

type purchase  {
	idPurchase
	idPerson
	ip
	device
	products
}

type Product {
	idProduct
	productName 
	price
}


<age>: int .
<device>: string .
<id>: string @index(exact) .
<idPerson>: string .
<idProduct>: string .
<idPurchase>: string .
<ip>: string @index(exact) .
<name>: string .
<price>: string .
<productName>: string .
<products>: [uid] .
<purchases>: [uid] @count @reverse .

So, I just need to fix my schema and set the same query?

I don’t think it was the schema. I suspect that your mutation was malformed. Based on the struct you shared, looks like it is all right, but maybe some typo in your go code led to some malformation in your dataset.

For example in your code, your product uid is “_:P”, I didn’t do that

This is a Blank Node. It is a identifier used during a transaction. So I can use it anywhere else in the mutation that it will be the same node. See this part of the Tour Mutation Introduction | Graphqlintro | Dgraph Tour.

In your case it isn’t necessary. Just specific cases. In your case you have to always use the same UID (of the product) for all new purchase. Otherwise you gonna create a new product every single new mutation.

I really appreciate your help

1 Like