easy-mongo-orm
Version:
A powerful and elegant MongoDB/Mongoose toolkit that makes database operations a breeze with built-in caching, search, pagination, performance monitoring, soft delete, versioning, data export/import, schema validation, and migration utilities
38 lines (37 loc) • 670 B
JavaScript
;
const efficientAggregation = async (Model, pipeline, options = {}) => {
const {
allowDiskUse = true,
batchSize = 1000,
maxTimeMS = 60000
} = options;
return Model.aggregate(pipeline).allowDiskUse(allowDiskUse).option({
maxTimeMS
}).option({
cursor: {
batchSize
}
});
};
const groupBy = field => ({
$group: {
_id: `$${field}`,
count: {
$sum: 1
}
}
});
const matchCondition = condition => ({
$match: condition
});
const sortByField = (field, direction = 1) => ({
$sort: {
[field]: direction
}
});
module.exports = {
efficientAggregation,
groupBy,
matchCondition,
sortByField
};