Skip to content
Open
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: 4 additions & 3 deletions lib/helpers/populate/assignVals.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ module.exports = function assignVals(o) {
valueToSet.$__.parent = docs[i];
}

const pathToSet = populateOptions.as || _path;
if (o.isVirtual && isDoc) {
docs[i].$populated(_path, o.justOne ? originalIds[0] : originalIds, o.allOptions);
// If virtual populate and doc is already init-ed, need to walk through
Expand All @@ -145,7 +146,7 @@ module.exports = function assignVals(o) {
valueToSet = valueToSet.map(v => v == null ? void 0 : v);
}
mpath.set(
_path,
pathToSet,
valueToSet,
docs[i],
// Handle setting paths underneath maps using $* by converting arrays into maps of values
Expand Down Expand Up @@ -212,7 +213,7 @@ module.exports = function assignVals(o) {
// If lean, need to check that each individual virtual respects
// `justOne`, because you may have a populated virtual with `justOne`
// underneath an array. See gh-6867
mpath.set(_path, valueToSet, docs[i], lookupLocalFields, setValue, false);
mpath.set(pathToSet, valueToSet, docs[i], lookupLocalFields, setValue, false);

if (docs[i].$__) {
markArraySubdocsPopulated(docs[i], [o.allOptions.options]);
Expand Down Expand Up @@ -284,7 +285,7 @@ function valueFilter(val, assignmentOpts, populateOptions, allIds) {
maybeRemoveId(subdoc, assignmentOpts);
ret.push(subdoc);
if (assignmentOpts.originalLimit &&
ret.length >= assignmentOpts.originalLimit) {
ret.length >= assignmentOpts.originalLimit) {
break;
}
}
Expand Down
97 changes: 50 additions & 47 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@
* @instance
*/

Model.prototype.$__version = function(where, delta) {
Model.prototype.$__version = function (where, delta) {

Check failure on line 634 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
const key = this.$__schema.options.versionKey;
if (where === true) {
// this is an insert
Expand Down Expand Up @@ -740,7 +740,7 @@

Model.prototype.deleteOne = function deleteOne(options) {
if (typeof options === 'function' ||
typeof arguments[1] === 'function') {
typeof arguments[1] === 'function') {
throw new MongooseError('Model.prototype.deleteOne() no longer accepts a callback');
}

Expand Down Expand Up @@ -911,7 +911,7 @@
* @api public
*/

Model.discriminator = function(name, schema, options) {
Model.discriminator = function (name, schema, options) {

Check failure on line 914 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
let model;
if (typeof name === 'function') {
model = name;
Expand Down Expand Up @@ -1042,7 +1042,7 @@
}

const conn = this.db;
const _ensureIndexes = async() => {
const _ensureIndexes = async () => {

Check failure on line 1045 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
const autoIndex = utils.getOption(
'autoIndex',
this.schema.options,
Expand All @@ -1054,7 +1054,7 @@
}
return await this.ensureIndexes({ _automatic: true });
};
const _createSearchIndexes = async() => {
const _createSearchIndexes = async () => {

Check failure on line 1057 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
const autoSearchIndex = utils.getOption(
'autoSearchIndex',
this.schema.options,
Expand All @@ -1067,7 +1067,7 @@

return await this.createSearchIndexes();
};
const _createCollection = async() => {
const _createCollection = async () => {

Check failure on line 1070 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
let autoCreate = utils.getOption(
'autoCreate',
this.schema.options,
Expand Down Expand Up @@ -1099,7 +1099,7 @@

const _catch = this.$init.catch;
const _this = this;
this.$init.catch = function() {
this.$init.catch = function () {

Check failure on line 1102 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
_this.$caught = true;
return _catch.apply(_this.$init, arguments);
};
Expand Down Expand Up @@ -1620,7 +1620,7 @@
let indexError;

options = options || {};
const done = function(err) {
const done = function (err) {

Check failure on line 1623 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
if (err && !model.$caught) {
model.emit('error', err);
}
Expand All @@ -1638,24 +1638,24 @@
}

if (!indexes.length) {
immediate(function() {
immediate(function () {

Check failure on line 1641 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
done();
});
return;
}
// Indexes are created one-by-one

const indexSingleDone = function(err, fields, options, name) {
const indexSingleDone = function (err, fields, options, name) {

Check failure on line 1648 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
model.emit('index-single-done', err, fields, options, name);
};
const indexSingleStart = function(fields, options) {
const indexSingleStart = function (fields, options) {

Check failure on line 1651 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
model.emit('index-single-start', fields, options);
};

const baseSchema = model.schema._baseSchema;
const baseSchemaIndexes = baseSchema ? baseSchema.indexes() : [];

immediate(function() {
immediate(function () {
// If buffering is off, do this manually.
if (options._automatic && !model.collection.collection) {
model.collection.addQueue(create, []);
Expand All @@ -1668,7 +1668,7 @@
function create() {
if (options._automatic) {
if (model.schema.options.autoIndex === false ||
(model.schema.options.autoIndex == null && model.db.config.autoIndex === false)) {
(model.schema.options.autoIndex == null && model.db.config.autoIndex === false)) {
return done();
}
}
Expand Down Expand Up @@ -2326,7 +2326,7 @@
* @api public
*/

Model.findOneAndUpdate = function(conditions, update, options) {
Model.findOneAndUpdate = function (conditions, update, options) {
_checkContext(this, 'findOneAndUpdate');
if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function' || typeof arguments[3] === 'function') {
throw new MongooseError('Model.findOneAndUpdate() no longer accepts a callback');
Expand Down Expand Up @@ -2413,7 +2413,7 @@
* @api public
*/

Model.findByIdAndUpdate = function(id, update, options) {
Model.findByIdAndUpdate = function (id, update, options) {
_checkContext(this, 'findByIdAndUpdate');
if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function' || typeof arguments[3] === 'function') {
throw new MongooseError('Model.findByIdAndUpdate() no longer accepts a callback');
Expand Down Expand Up @@ -2466,7 +2466,7 @@
* @api public
*/

Model.findOneAndDelete = function(conditions, options) {
Model.findOneAndDelete = function (conditions, options) {
_checkContext(this, 'findOneAndDelete');

if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
Expand Down Expand Up @@ -2503,7 +2503,7 @@
* @see mongodb https://www.mongodb.com/docs/manual/reference/command/findAndModify/
*/

Model.findByIdAndDelete = function(id, options) {
Model.findByIdAndDelete = function (id, options) {
_checkContext(this, 'findByIdAndDelete');

if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
Expand Down Expand Up @@ -2546,7 +2546,7 @@
* @api public
*/

Model.findOneAndReplace = function(filter, replacement, options) {
Model.findOneAndReplace = function (filter, replacement, options) {
_checkContext(this, 'findOneAndReplace');

if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function' || typeof arguments[3] === 'function') {
Expand Down Expand Up @@ -2597,7 +2597,7 @@

Model.create = async function create(doc, options) {
if (typeof options === 'function' ||
typeof arguments[2] === 'function') {
typeof arguments[2] === 'function') {
throw new MongooseError('Model.create() no longer accepts a callback');
}

Expand Down Expand Up @@ -2629,12 +2629,12 @@
}

if (args.length === 2 &&
args[0] != null &&
args[1] != null &&
args[0].session == null &&
last &&
getConstructorName(last.session) === 'ClientSession' &&
!this.schema.path('session')) {
args[0] != null &&
args[1] != null &&
args[0].session == null &&
last &&
getConstructorName(last.session) === 'ClientSession' &&
!this.schema.path('session')) {
// Probably means the user is running into the common mistake of trying
// to use a spread to specify options, see gh-7535
utils.warn('WARNING: to pass a `session` to `Model.create()` in ' +
Expand Down Expand Up @@ -2664,7 +2664,7 @@
this;
if (Model == null) {
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
`found for model "${this.modelName}"`);
`found for model "${this.modelName}"`);
}
let toSave = doc;
if (!(toSave instanceof Model)) {
Expand All @@ -2689,7 +2689,7 @@
this;
if (Model == null) {
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
`found for model "${this.modelName}"`);
`found for model "${this.modelName}"`);
}
let toSave = doc;

Expand All @@ -2710,7 +2710,7 @@
this;
if (Model == null) {
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
`found for model "${this.modelName}"`);
`found for model "${this.modelName}"`);
}
try {
let toSave = doc;
Expand Down Expand Up @@ -2817,7 +2817,7 @@
* @api public
*/

Model.watch = function(pipeline, options) {
Model.watch = function (pipeline, options) {
_checkContext(this, 'watch');

options = options || {};
Expand Down Expand Up @@ -2873,7 +2873,7 @@
* @api public
*/

Model.startSession = function() {
Model.startSession = function () {
_checkContext(this, 'startSession');

return this.db.startSession.apply(this.db, arguments);
Expand Down Expand Up @@ -2995,7 +2995,7 @@
}

// We filter all failed pre-validations by removing nulls
const docAttributes = docs.filter(function(doc) {
const docAttributes = docs.filter(function (doc) {
return doc != null;
});
for (let i = 0; i < docAttributes.length; ++i) {
Expand Down Expand Up @@ -3033,7 +3033,7 @@
}
return [];
}
const docObjects = lean ? docAttributes : docAttributes.map(function(doc) {
const docObjects = lean ? docAttributes : docAttributes.map(function (doc) {
if (doc.$__schema.options.versionKey) {
doc[doc.$__schema.options.versionKey] = 0;
}
Expand All @@ -3054,7 +3054,7 @@
// `writeErrors` is a property reported by the MongoDB driver,
// just not if there's only 1 error.
if (error.writeErrors == null &&
(error.result && error.result.result && error.result.result.writeErrors) != null) {
(error.result && error.result.result && error.result.result.writeErrors) != null) {
error.writeErrors = error.result.result.writeErrors;
}

Expand Down Expand Up @@ -3281,7 +3281,7 @@
_checkContext(this, 'bulkWrite');

if (typeof options === 'function' ||
typeof arguments[2] === 'function') {
typeof arguments[2] === 'function') {
throw new MongooseError('Model.bulkWrite() no longer accepts a callback');
}
options = options || {};
Expand Down Expand Up @@ -3830,7 +3830,7 @@
* @api public
*/

Model.hydrate = function(obj, projection, options) {
Model.hydrate = function (obj, projection, options) {
_checkContext(this, 'hydrate');

if (options?.virtuals && options?.hydratedPopulatedDocs === false) {
Expand Down Expand Up @@ -4004,9 +4004,9 @@
options = typeof options === 'function' ? options : clone(options);

const versionKey = model &&
model.schema &&
model.schema.options &&
model.schema.options.versionKey || null;
model.schema &&
model.schema.options &&
model.schema.options.versionKey || null;
decorateUpdateWithVersionKey(doc, options, versionKey);

return mq[op](conditions, doc, options);
Expand Down Expand Up @@ -4331,6 +4331,9 @@
ids = utils.array.unique(ids);

const assignmentOpts = {};
if (populateOptions.as) {
assignmentOpts.as = populateOptions.as;
}
assignmentOpts.sort = mod &&
mod.options &&
mod.options.options &&
Expand All @@ -4341,9 +4344,9 @@
// to fail. So delay running lean transform until _after_
// `_assign()`
if (mod.options &&
mod.options.options &&
mod.options.options.lean &&
mod.options.options.lean.transform) {
mod.options.options &&
mod.options.options.lean &&
mod.options.options.lean.transform) {
mod.options.options._leanTransform = mod.options.options.lean.transform;
mod.options.options.lean = true;
}
Expand Down Expand Up @@ -4475,8 +4478,8 @@
// field, that's the client's fault.
for (const foreignField of mod.foreignField) {
if (foreignField !== '_id' &&
query.selectedInclusively() &&
!isPathSelectedInclusive(query._fields, foreignField)) {
query.selectedInclusively() &&
!isPathSelectedInclusive(query._fields, foreignField)) {
query.select(foreignField);
}
}
Expand Down Expand Up @@ -4614,7 +4617,7 @@
assignVals({
originalModel: model,
// If virtual, make sure to not mutate original field
rawIds: mod.isVirtual ? allIds : mod.allIds,
rawIds: (mod.isVirtual || assignmentOpts.as) ? allIds : mod.allIds,
allIds: allIds,
unpopulatedValues: mod.unpopulatedValues,
foreignField: mod.foreignField,
Expand Down Expand Up @@ -4741,7 +4744,7 @@
model.$__collection = collection;

// Create custom query constructor
model.Query = function() {
model.Query = function () {
Query.apply(this, arguments);
};
Object.setPrototypeOf(model.Query.prototype, Query.prototype);
Expand Down Expand Up @@ -4891,7 +4894,7 @@
Model.collection = Model.prototype.collection;
Model.$__collection = Model.collection;
// Errors handled internally, so ignore
Model.init().catch(() => {});
Model.init().catch(() => { });
return Model;
};

Expand Down Expand Up @@ -4947,7 +4950,7 @@
* @api public
*/

Model.inspect = function() {
Model.inspect = function () {
return `Model { ${this.modelName} }`;
};

Expand Down
Loading