So I have the following query for about 500k documents and it is really slow on the database side:
db.collection('news').aggregate(
[
{
$match: {
feeds: { $in: feeds },
createdAt: { $lte: lastPostDate },
deleted: {
$exists: false
}
}
},
{
$group: {
_id: { title: '$title' },
id: { $first: '$_id' },
title: { $addToSet: '$title' },
source: { $first: '$source' },
shortid: { $first: '$shortid' },
stats: { $first: '$stats' },
log: { $first: '$log' },
createdAt: { $first: '$createdAt' },
feedUpdated: { $first: '$feedUpdated' },
media: { $first: '$media' },
title: { $first: '$title' }
}
},
{ $sort: sort },
{
$skip: limit * (page - 1)
},
{
$limit: limit * 1
}
],
{
allowDiskUse: true,
cursor: {}
}
)
I know the allowDiskUse is making it slow, but if I disable it I get:
MongoError: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.