I have next structure of my mongoDB collection:
{
"_id": "1",
"sessions": [
{
"type": "default",
"pageViews": [
{
"path": "/",
"host": "example.com",
"events": [
{
"name": "event1"
},
{
"name": "event1"
}
]
}
]
},
{
"type": "default",
"pageViews": [
{
"path": "/",
"host": "example.com",
"events": [
{
"name": "event1"
},
{
"name": "event1"
}
]
}
]
}
]
}
I need to get count of sessions, pageViews and events in one query grouping by _id field.
I do:
db.collection.aggregate([
{
$unwind: "$sessions"
},
{
$unwind: "$sessions.pageViews"
},
{
$group: {
_id : "$_id",
totalEvents: {
$sum: {
$size: "$sessions.pageViews.events"
},
},
totalPageViews: {
$sum: 1
}
}
}
])
But I can't understand how to get count of sessions.
How can I get count of sessions in this query ?
Thanks !