In my MongoDB I have a team
collection and a group
collection.
Here is an example of a team document:
'_id': ObjectId('62724a2effffbd4c82eafb1b'),
'name': 'Team A'
And here is an example of a group document:
'_id': ObjectId('6139f790f6a0af36d700c4fb'),
'name': 'Test Group',
'teams': [
{
'division': 1,
'teamId': ObjectId('62724a2effffbd4c82eafb1b')
},
{
'division': 1,
'teamId': ObjectId('6139f795f6a0af373900e2f7')
},
...
]
I'm trying to make an aggregation query that returns the following data:
- A list of teams that is part of
Test Group
, paged with up to 100 teams. - A count of how many teams that was found in total.
- A
first
boolean property that's true if paging is on the first page. - A
last
boolean property that's true if paging is on the last page.
The query should of course take a page number and a pagesize number. The returned value should look like this:
{
'content': [
{
'id': '62724a2effffbd4c82eafb1b',
'name': 'Team A'
},
{
'id': '6139f795f6a0af373900e2f7',
'name': 'Whatever Team'
},
...
],
'last': false,
'first': true,
'totalElements': 132
}
What I have so far:
{
$match: {
_id: ObjectId('6139f790f6a0af36d700c4fb')
}
},
{
$lookup: {
from: "team",
localField: "teams.teamId",
foreignField: "_id",
as: "teams"
}
}
From here I've tried a few different things. I've tried to use the facet operator to build up the different properties, but then I can't fit the paging part in. I just don't know which way to go from here.