没有简单的方法。您需要将整个集合分组到可能需要的数组中
allowDiskUse
对于具有巨大性能影响的大型数据集。
db.collection.aggregate([
// count all documents
{ $group: {
_id: null,
cnt: { $sum: 1},
docs: { $push: "$$ROOT" }
} },
// add _batch field to group documents by
{ $project: {
_id: 0,
docs: { $map: {
// add a sequential number to each
input: { $zip: {
inputs: [ "$docs", { $range: [ 0, "$cnt" ] } ]
} },
as: "doc",
in: { $mergeObjects: [
{ $arrayElemAt: [ "$$doc", 0 ] },
// split it in batches by 4 based on the sequential number
{ _batch: { $cond: [
{ $eq: [ { $arrayElemAt: [ "$$doc", 1 ] }, 0 ] },
1,
{ $ceil: { $divide: [ { $arrayElemAt: [ "$$doc", 1 ] }, 4 ] } }
] } }
] }
} }
} },
{ $unwind: "$docs" },
{ $replaceRoot: { newRoot: "$docs" } },
// ensure original order, only if you need ItemRange as a string
{ $sort: { _id: 1 } },
// calculate averages per batch
{ $group: {
_id: "$_batch",
start: { $first: "$ItemName" }, // only if you need ItemRange as a string
end: { $last: "$ItemName" }, // only if you need ItemRange as a string
RatingAvg: {$avg: "$Rating"}
} },
// only if you need them in order
{ $sort: { _id: 1 } },
// calculate ItemRange, only if you need ItemRange as a string
{ $project: {
_id: 0,
ItemRange: { $concat: [ "$start", "-", "$end" ] },
RatingAvg: 1
} },
])
不确定实际用例,因为删除第一个文档时,所有平均值都会改变。
不管怎样,如果您不需要格式为“FirstName LastName”的ItemRange,而是可以使用批号,那么您可以跳过2个last in memory排序,这将提高性能。