This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -7,7 +7,9 @@
const AggregationCursor = require('./cursor/AggregationCursor');
const Query = require('./query');
const applyGlobalMaxTimeMS = require('./helpers/query/applyGlobalMaxTimeMS');
const getConstructorName = require('./helpers/getConstructorName');
const promiseOrCallback = require('./helpers/promiseOrCallback');
const stringifyFunctionOperators = require('./helpers/aggregate/stringifyFunctionOperators');
const util = require('util');
const utils = require('./utils');
const read = Query.prototype.read;
@@ -124,7 +126,7 @@ Aggregate.prototype.model = function(model) {
* aggregate.append({ $project: { field: 1 }}, { $limit: 2 });
*
* // or pass an array
* var pipeline = [{ $match: { daw: 'Logic Audio X' }} ];
* const pipeline = [{ $match: { daw: 'Logic Audio X' }} ];
* aggregate.append(pipeline);
*
* @param {Object} ops operator(s) to append
@@ -470,7 +472,7 @@ Aggregate.prototype.sortByCount = function(arg) {
};
/**
* Appends new custom $lookup operator(s) to this aggregate pipeline.
* Appends new custom $lookup operator to this aggregate pipeline.
*
* ####Examples:
*
@@ -478,8 +480,7 @@ Aggregate.prototype.sortByCount = function(arg) {
*
* @see $lookup https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/#pipe._S_lookup
* @param {Object} options to $lookup as described in the above link
* @return {Aggregate}
* @api public
* @return {Aggregate}* @api public
*/
Aggregate.prototype.lookup = function(options) {
@@ -522,7 +523,7 @@ Aggregate.prototype.graphLookup = function(options) {
};
/**
* Appends new custom $sample operator(s) to this aggregate pipeline.
* Appends new custom $sample operator to this aggregate pipeline.
*
* ####Examples:
*
@@ -562,7 +563,7 @@ Aggregate.prototype.sort = function(arg) {
const sort = {};
if (arg.constructor.name === 'Object') {
if (getConstructorName(arg) === 'Object') {
const desc = ['desc', 'descending', -1];
Object.keys(arg).forEach(function(field) {
// If sorting by text score, skip coercing into 1/-1
@@ -792,7 +793,7 @@ Aggregate.prototype.session = function(session) {
*
* ####Example:
*
* var agg = Model.aggregate(..).option({ allowDiskUse: true }); // Set the `allowDiskUse` option
* const agg = Model.aggregate(..).option({ allowDiskUse: true }); // Set the `allowDiskUse` option
* agg.options; // `{ allowDiskUse: true }`
*
* @param {Object} options keys to merge into current options
@@ -819,7 +820,7 @@ Aggregate.prototype.option = function(value) {
*
* ####Example:
*
* var cursor = Model.aggregate(..).cursor({ batchSize: 1000 }).exec();
* const cursor = Model.aggregate(..).cursor({ batchSize: 1000 }).exec();
* cursor.eachAsync(function(doc, i) {
* // use doc
* });
@@ -909,6 +910,32 @@ Aggregate.prototype.facet = function(options) {
return this.append({ $facet: options });
};
/**
* Helper for [Atlas Text Search](https://docs.atlas.mongodb.com/reference/atlas-search/tutorial/)'s
* `$search` stage.
*
* ####Example:
*
* Model.aggregate().
* search({
* text: {
* query: 'baseball',
* path: 'plot'
* }
* });
*
* // Output: [{ plot: '...', title: '...' }]
*
* @param {Object} $search options
* @return {Aggregate} this
* @see $search https://docs.atlas.mongodb.com/reference/atlas-search/tutorial/
* @api public
*/
Aggregate.prototype.search = function(options) {
return this.append({ $search: options });
};
/**
* Returns the current pipeline
*
@@ -933,7 +960,7 @@ Aggregate.prototype.pipeline = function() {
* aggregate.exec(callback);
*
* // Because a promise is returned, the `callback` is optional.
* var promise = aggregate.exec();
* const promise = aggregate.exec();
* promise.then(..);
*
* @see Promise #promise_Promise
@@ -956,8 +983,8 @@ Aggregate.prototype.exec = function(callback) {
}
return promiseOrCallback(callback, cb => {
prepareDiscriminatorPipeline(this);
stringifyFunctionOperators(this._pipeline);
model.hooks.execPre('aggregate', this, error => {
if (error) {
@@ -1027,7 +1054,7 @@ Aggregate.prototype.catch = function(reject) {
};
/**
* Returns an asyncIterator for use with [`for/await/of` loops](http://bit.ly/async-iterators)
* Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js
* You do not need to call this function explicitly, the JavaScript runtime
* will call it for you.
*
@@ -1056,9 +1083,7 @@ if (Symbol.asyncIterator != null) {
return this.cursor({ useMongooseAggCursor: true }).
exec().
transformNull().
map(doc => {
return doc == null ? { done: true } : { value: doc, done: false };
});
_transformForAsyncIterator();
};
}
@@ -1114,6 +1139,14 @@ function prepareDiscriminatorPipeline(aggregate) {
originalPipeline[0].$geoNear.query =
originalPipeline[0].$geoNear.query || {};
originalPipeline[0].$geoNear.query[discriminatorKey] = discriminatorValue;
} else if (originalPipeline[0] && originalPipeline[0].$search) {
if (originalPipeline[1] && originalPipeline[1].$match != null) {
originalPipeline[1].$match[discriminatorKey] = originalPipeline[1].$match[discriminatorKey] || discriminatorValue;
} else {
const match = {};
match[discriminatorKey] = discriminatorValue;
originalPipeline.splice(1, 0, { $match: match });
}
} else {
const match = {};
match[discriminatorKey] = discriminatorValue;