Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,15 +741,10 @@ function init(self, obj, doc, opts, prefix) {
let schemaType;
let path;
let i;
let index = 0;
const strict = self.$__.strictMode;
const docSchema = self.$__schema;

while (index < len) {
_init(index++);
}

function _init(index) {
for (let index = 0; index < len; ++index) {
i = keys[index];
// avoid prototype pollution
if (i === '__proto__' || i === 'constructor') {
Expand Down
56 changes: 33 additions & 23 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ const queryOptionMethods = new Set([
'wtimeout'
]);

// Map from operation name to the name of the function that executes the actual operation against MongoDB.
// Called a thunk for legacy reasons, "thunk" means function that takes exactly 1 param, a callback.
// Currently `_countDocuments()`, etc. are async functions that take no params.
const opToThunk = new Map([
['countDocuments', '_countDocuments'],
['distinct', '__distinct'],
['estimatedDocumentCount', '_estimatedDocumentCount'],
['find', '_find'],
['findOne', '_findOne'],
['findOneAndReplace', '_findOneAndReplace'],
['findOneAndUpdate', '_findOneAndUpdate'],
['replaceOne', '_replaceOne'],
['updateMany', '_updateMany'],
['updateOne', '_updateOne'],
['deleteMany', '_deleteMany'],
['deleteOne', '_deleteOne'],
['findOneAndDelete', '_findOneAndDelete']
]);

/**
* Query constructor used for building queries. You do not need
* to instantiate a `Query` directly. Instead use Model functions like
Expand Down Expand Up @@ -2337,18 +2356,17 @@ Query.prototype._find = async function _find() {
}

const mongooseOptions = this._mongooseOptions;
const _this = this;
const userProvidedFields = _this._userProvidedFields || {};
const userProvidedFields = this._userProvidedFields || {};

applyGlobalMaxTimeMS(this.options, this.model.db.options, this.model.base.options);
applyGlobalDiskUse(this.options, this.model.db.options, this.model.base.options);

// Separate options to pass down to `completeMany()` in case we need to
// set a session on the document
const completeManyOptions = Object.assign({}, {
const completeManyOptions = {
session: this && this.options && this.options.session || null,
lean: mongooseOptions.lean || null
});
};

const options = this._optionsForExec();

Expand All @@ -2366,7 +2384,7 @@ Query.prototype._find = async function _find() {
}

if (!mongooseOptions.populate) {
const versionKey = _this.schema.options.versionKey;
const versionKey = this.schema.options.versionKey;
if (mongooseOptions.lean && mongooseOptions.lean.versionKey === false && versionKey) {
docs.forEach((doc) => {
if (versionKey in doc) {
Expand All @@ -2375,17 +2393,17 @@ Query.prototype._find = async function _find() {
});
}
return mongooseOptions.lean ?
_completeManyLean(_this.model.schema, docs, null, completeManyOptions) :
_this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
_completeManyLean(this.model.schema, docs, null, completeManyOptions) :
this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
}

const pop = helpers.preparePopulationOptionsMQ(_this, mongooseOptions);
const pop = helpers.preparePopulationOptionsMQ(this, mongooseOptions);

if (mongooseOptions.lean) {
return _this.model.populate(docs, pop);
return this.model.populate(docs, pop);
}

docs = await _this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
docs = await this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
await this.model.populate(docs, pop);

return docs;
Expand Down Expand Up @@ -4397,22 +4415,14 @@ Query.prototype.exec = async function exec(op) {
if (this.model == null) {
throw new MongooseError('Query must have an associated model before executing');
}
this._validateOp();

if (!this.op) {
return;
}

if (this.options && this.options.sort) {
const keys = Object.keys(this.options.sort);
if (keys.includes('')) {
throw new Error('Invalid field "" passed to sort()');
}
const thunk = opToThunk.get(this.op);
if (!thunk) {
throw new MongooseError('Query has invalid `op`: "' + this.op + '"');
}

let thunk = '_' + this.op;
if (this.op === 'distinct') {
thunk = '__distinct';
if (this.options && this.options.sort && typeof this.options.sort === 'object' && this.options.sort.hasOwnProperty('')) {
throw new Error('Invalid field "" passed to sort()');
}

if (this._executionStack != null) {
Expand Down
Loading