{
author: "users/...", // An _id of a user
kind: 1, // 1 or 2
}{
username: "...",
kind: 1, // 1 or 2
}FOR item IN items FILTER item.owner == @user LET author = ( FOR author IN users FILTER item.author == author._id LIMIT 1 RETURN author ) COLLECT WITH COUNT INTO total RETURN { total: total, users: { kind1: <?>, kind2: <?> }, stats: { stat1: <?>, stat2: <?> } }LET list = (FOR item IN items FILTER item.owner == @user LET author = ( FOR author IN users FILTER item.author == author._id LIMIT 1 RETURN author )
RETURN {
userKind1: (author[0].kind == 1 ? 1 : 0),
userKind2: (author[0].kind == 2 ? 1 : 0),
stat1: (item.kind == 1 ? 1 : 0),
stat2: (item.kind == 2 ? 1 : 0)
}
)
FOR entry IN list
COLLECT tmp = entry.nomatter INTO c
RETURN { total: LENGTH(c), users: { kind1: SUM(c[*].userKind1), kind2: SUM(c[*].userKind2) }, stats: { stat1: SUM(c[*].stat1), stat2: SUM(c[*].stat2) } }// sub-query is required in order to do multiple things with the results
LET kinds = (
FOR item IN items
FILTER item.owner == @owner
LET authorKind = (
FOR user IN users
FILTER item.author == user._id // join item <-> user
LIMIT 1
RETURN user.kind // no need to return the whole user document
)
RETURN {
itemKind: item.kind, // no need to return the whole item document
userKind: authorKind[0] // query results are always arrays
}
)
RETURN {
total: LENGTH(kinds), // in above sub-query, we return one result per *item*
users: (
FOR k IN kinds
COLLECT kind = k.userKind WITH COUNT INTO kc // count how often every user kind appears
RETURN { [ CONCAT("kind", kind) ]: kc } // instead of a literal name, compute it dynamically
),
stats: (
FOR k IN kinds
COLLECT kind = k.itemKind WITH COUNT INTO kc
RETURN { [ CONCAT("stat", kind) ]: kc }
// you could also return a different data structure, with static attribute keys:
//RETURN {kind: kind, count: kc}
)
}[
{
"total": 11,
"stats": [
{
"stat1": 7
},
{
"stat2": 4
}
],
"users": [
{
"kind0": 5
},
{
"kind1": 6
}
]
}
][
[
{ "kind": 1, "username": "Heinz", "_id": "users/heinz" },
{ "kind": 0, "username": "Jessica", "_id": "users/jessica" },
{ "kind": 0, "username": "Walter", "_id": "users/walter" },
{ "kind": 1, "username": "John", "_id": "users/john" },
{ "kind": 1, "username": "Jessie", "_id": "users/jessie" },
{ "kind": 2, "username": "Kingsley", "_id": "users/kingsley" },
{ "kind": 1, "username": "Max", "_id": "users/max" },
{ "kind": 0, "username": "Caroline", "_id": "users/caroline" },
{ "kind": 1, "username": "Anna", "_id": "users/anna" }
]
][
[
{ "kind": 2, "author": "users/john", "owner": "users/john", "_id": "items/1020494" },
{ "kind": 1, "author": "users/jessie", "owner": "users/kingsley", "_id": "items/1020480" },
{ "kind": 2, "author": "users/walter", "owner": "users/kingsley", "_id": "items/1020474" },
{ "kind": 2, "author": "users/john", "owner": "users/john", "_id": "items/1020470" },
{ "kind": 1, "author": "users/jessica", "owner": "users/kingsley", "_id": "items/1020482" },
{ "kind": 1, "author": "users/jessica", "owner": "users/john", "_id": "items/1020490" },
{ "kind": 2, "author": "users/heinz", "owner": "users/john", "_id": "items/1020472" },
{ "kind": 1, "author": "users/max", "owner": "users/kingsley", "_id": "items/1020466" },
{ "kind": 2, "author": "users/jessie", "owner": "users/john", "_id": "items/1020496" },
{ "kind": 2, "author": "users/caroline", "owner": "users/kingsley", "_id": "items/1020498" },
{ "kind": 1, "author": "users/anna", "owner": "users/kingsley", "_id": "items/1020476" },
{ "kind": 1, "author": "users/walter", "owner": "users/kingsley", "_id": "items/1020478" },
{ "kind": 1, "author": "users/jessica", "owner": "users/kingsley", "_id": "items/1020468" },
{ "kind": 2, "author": "users/walter", "owner": "users/john", "_id": "items/1020460" },
{ "kind": 2, "author": "users/john", "owner": "users/kingsley", "_id": "items/1020488" },
{ "kind": 2, "author": "users/max", "owner": "users/john", "_id": "items/1020486" },
{ "kind": 1, "author": "users/jessie", "owner": "users/kingsley", "_id": "items/1020464" },
{ "kind": 2, "author": "users/jessie", "owner": "users/kingsley", "_id": "items/1020500" },
{ "kind": 2, "author": "users/jessica", "owner": "users/john", "_id": "items/1020492" },
{ "kind": 2, "author": "users/caroline", "owner": "users/john", "_id": "items/1020484" }
]
]{"owner": "users/kingsley"}[
{
"total": 9,
"users": [
{
"kind0": 4
},
{
"kind1": 5
}
],
"stats": [
{
"stat1": 1
},
{
"stat2": 8
}
]
}
]RETURN {
total: LENGTH(kinds),
users: MERGE(
FOR k IN kinds
COLLECT kind = k.userKind WITH COUNT INTO kc
RETURN { [ CONCAT("kind", kind) ]: kc }
),
stats: MERGE(
FOR k IN kinds
COLLECT kind = k.itemKind WITH COUNT INTO kc
RETURN { [ CONCAT("stat", kind) ]: kc }
)
}