As promised here is a basic example. The structure itself is pretty basic indeed. Each date will be a node (uid). That way you can use GroupBy perfectly.
With the next version of Dgraph approaching, the new upsert transaction will make it easier for you to use this structure. Because you can check if the date already exists, if it does not exist it creates a new one and at the same time attach both objects.
BTW, the use of this structure does not prevent you from using datetime indexing. They are two different things. The structure will serve for you to “visually” analyze your stats and help with groupby()
.
You do not need an established structure at first, because you just need dates to be nodes. But I find it convenient to have a structure. And you can use the same logic to other things.
Mutation in JSON and Schema
date: datetime .
acquired_when: [uid] @reverse .
name: String @index(exact) .
[
{
"name": "Warren Buffett",
"user": "",
"u.asset": [
{
"asset": "Duracell",
"acquired_when": [
{
"uid": "_:2018-01-28"
}
]
},
{
"asset": "Geico",
"acquired_when": [
{
"uid": "_:2018-01-28"
}
]
},
{
"asset": "NetJets",
"acquired_when": [
{
"uid": "_:2018-02-14"
}
]
},
{
"asset": "Brooks",
"acquired_when": [
{
"uid": "_:2018-03-01"
}
]
},
{
"asset": "DairyQueen",
"acquired_when": [
{
"uid": "_:2018-06-04"
}
]
}
]
},
{
"year": "2018",
"month": [
{
"month_name": "jan",
"day": [
{
"date": "2018-01-28",
"uid": "_:2018-01-28"
}
]
},
{
"month_name": "feb",
"day": [
{
"date": "2018-02-14",
"uid": "_:2018-02-14"
}
]
},
{
"month_name": "mar",
"day": [
{
"date": "2018-03-01",
"uid": "_:2018-03-01"
}
]
}
]
}
]
Queries
{
var(func: has(acquired_when)) @groupby(acquired_when) {
A as count(uid)
}
q(func: uid(A), orderdesc: val(A)){
uid
date
total : val(A)
acquired_in_this_date : ~acquired_when{
uid
asset
}
}
}
Result:
{
"data": {
"q": [
{
"uid": "0x6",
"date": "2018-01-28",
"total": 2,
"acquired_in_this_date": [
{
"uid": "0x1",
"asset": "Duracell"
},
{
"uid": "0x9",
"asset": "Geico"
}
]
},
{
"uid": "0x2",
"date": "2018-03-01",
"total": 1,
"acquired_in_this_date": [
{
"uid": "0x7",
"asset": "Brooks"
}
]
},
{
"uid": "0x3",
"total": 1,
"acquired_in_this_date": [
{
"uid": "0x8",
"asset": "DairyQueen"
}
]
},
{
"uid": "0xa",
"date": "2018-02-14",
"total": 1,
"acquired_in_this_date": [
{
"uid": "0xd",
"asset": "NetJets"
}
]
}
]
}
Query to view your structure
{
q(func: has(year)) {
uid
month {
uid
month_name
day {
date
uid
}
}
}
}