Answer the question
In order to leave comments, you need to log in
How to select subdocuments with pagination from a collection?
Good day.
There is a collection, let's call it Reports.
{
_id: '123123123',
name: 'Report 1',
userStatuses: [
{
userId: '1234567',
userEmail: '[email protected]`
},
{
userId: '1234567',
userEmail: '[email protected]`
},
],
createdAt: '2021-05-14 06:01:25.444Z'
}
{
"_id" : "YGh2gkct9C4cNce4j",
"email" : "[email protected]",
"external_id" : "12345678",
"userAccessLevel" : "user",
"createdAt" : "2019-08-13T20:16:07.175Z"
}
db.Reports.rawCollection().aggregate(
[
{
$match: {
'createdAt': {
$gte: dateRangeStart.toDate(),
$lt: dateRangeEnd.toDate(),
},
}
},
{
$lookup: {
from: 'UsersMap',
as: 'map',
let: { userId: '$userStatuses.userId' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$_id', '$$userId'] },
{ $eq: ['$userAccessLevel', 'user'] },
]
}
}
}
]
},
]
);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question