问题
I have a collection having 1Million documents... I have passed the option for allowDiskUse
and now it is throwing error
TypeError: callback.apply is not a function
I have searched for this but could get the solution... Please help
const pictures = await Picture.aggregate([
{ $sort: { createdAt: -1 }},
{ $group: {
_id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } },
pictures: {
$push: {
_id: '$_id',
image: '$image',
time: { $dateToString: { format: "%H:%M:%S", date: "$createdAt" } }
}
}
}},
{ $limit: 50 },
{ $project: {
_id: false,
createdAt: '$_id',
pictures: '$pictures'
}}
], { allowDiskUse: true })
Mongodb version - 3.6
Mongoose version - 5.0
回答1:
Because this is "mongoose". There is no "options" block on the aggregate()
method in the Mongoose API. That's the source link and then the documentation. Note the returned <Aggregate> type.
That chains to allowDiskUse(true) as demonstrated in the documentation:
await Model.aggregate(..).allowDiskUse(true).exec()
You should really never need to use the option in most aggregations. Getting a warning message is usually an indicator that you are actually missing an index, or indeed any sane attempt to $match and filter down results.
来源:https://stackoverflow.com/questions/50756344/typeerror-callback-apply-is-not-a-function-after-allowdiskuse