Skip to content

Commit

Permalink
Merge branch '6.0' into gh-9840
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 30, 2021
2 parents 0bd31aa + 6d1d5f1 commit c1595d0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
2 changes: 0 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2962,15 +2962,13 @@ Document.prototype.$__undoReset = function $__undoReset() {

Document.prototype.$__dirty = function() {
const _this = this;

let all = this.$__.activePaths.map('modify', function(path) {
return {
path: path,
value: _this.$__getValue(path),
schema: _this.$__path(path)
};
});

// gh-2558: if we had to set a default and the value is not undefined,
// we have to save as well
all = all.concat(this.$__.activePaths.map('default', function(path) {
Expand Down
9 changes: 5 additions & 4 deletions lib/helpers/populate/getModelsMapForPopulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
justOne = options.justOne;
} else if (schema && !schema[schemaMixedSymbol]) {
// Skip Mixed types because we explicitly don't do casting on those.
justOne = Array.isArray(schema) ?
schema.every(schema => !schema.$isMongooseArray) :
!schema.$isMongooseArray;
if (options.path.endsWith('.' + schema.path) || options.path === schema.path) {
justOne = Array.isArray(schema) ?
schema.every(schema => !schema.$isMongooseArray) :
!schema.$isMongooseArray;
}
}

if (!modelNames) {
Expand Down Expand Up @@ -173,7 +175,6 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
return err;
}
}

return map;

function _getModelNames(doc, schema, modelNameFromQuery) {
Expand Down
7 changes: 0 additions & 7 deletions lib/helpers/populate/getSchemaTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const populateModelSymbol = require('../symbols').populateModelSymbol;
module.exports = function getSchemaTypes(model, schema, doc, path) {
const pathschema = schema.path(path);
const topLevelDoc = doc;

if (pathschema) {
return pathschema;
}
Expand All @@ -53,7 +52,6 @@ module.exports = function getSchemaTypes(model, schema, doc, path) {
while (p--) {
trypath = parts.slice(0, p).join('.');
foundschema = schema.path(trypath);

if (foundschema == null) {
continue;
}
Expand Down Expand Up @@ -137,7 +135,6 @@ module.exports = function getSchemaTypes(model, schema, doc, path) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
!foundschema.schema.$isSingleNested;
}

return ret;
}
} else if (p !== parts.length &&
Expand Down Expand Up @@ -173,7 +170,6 @@ module.exports = function getSchemaTypes(model, schema, doc, path) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
!model.schema.$isSingleNested;
}

return ret;
}
}
Expand All @@ -197,15 +193,12 @@ module.exports = function getSchemaTypes(model, schema, doc, path) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
!schema.$isSingleNested;
}

return ret;
}
}

return foundschema;
}
}

// look for arrays
const parts = path.split('.');
for (let i = 0; i < parts.length; ++i) {
Expand Down
6 changes: 0 additions & 6 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,6 @@ Model.prototype.$__delta = function() {
for (; d < len; ++d) {
const data = dirty[d];
let value = data.value;

const match = checkDivergentArray(this, data.path, value);
if (match) {
divergent.push(match);
Expand Down Expand Up @@ -714,7 +713,6 @@ Model.prototype.$__delta = function() {
}

if (divergent.length) continue;

if (value === undefined) {
operand(this, where, delta, data, 1, '$unset');
} else if (value === null) {
Expand Down Expand Up @@ -4265,7 +4263,6 @@ function populate(model, docs, options, callback) {
}

const modelsMap = getModelsMapForPopulate(model, docs, options);

if (modelsMap instanceof MongooseError) {
return immediate(function() {
callback(modelsMap);
Expand Down Expand Up @@ -4349,7 +4346,6 @@ function populate(model, docs, options, callback) {
} else if (mod.options.limit != null) {
assignmentOpts.originalLimit = mod.options.limit;
}

params.push([mod, match, select, assignmentOpts, _next]);
}

Expand All @@ -4372,7 +4368,6 @@ function populate(model, docs, options, callback) {
for (const arr of params) {
_execPopulateQuery.apply(null, arr);
}

function _next(err, valsFromDb) {
if (err != null) {
return callback(err, null);
Expand Down Expand Up @@ -4406,7 +4401,6 @@ function populate(model, docs, options, callback) {

function _execPopulateQuery(mod, match, select, assignmentOpts, callback) {
const subPopulate = utils.clone(mod.options.populate);

const queryOptions = Object.assign({
skip: mod.options.skip,
limit: mod.options.limit,
Expand Down
3 changes: 3 additions & 0 deletions lib/types/subdocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function Subdocument(value, fields, parent, skipId, options) {
const parentOptions = { isNew: parent.isNew, defaults: parent.$__.$options.defaults };
options = Object.assign({}, parentOptions, options);
}
if (options != null && options.path != null) {
this.$basePath = options.path;
}
Document.call(this, value, fields, skipId, options);

delete this.$__.$options.priorDoc;
Expand Down
38 changes: 38 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9879,4 +9879,42 @@ describe('model: populate:', function() {
assert.deepEqual(findCallOptions[0].virtuals, ['foo']);
});
});
it('gh-9833', function() {
const Books = db.model('books', new Schema({ name: String, tags: [{ type: Schema.Types.ObjectId, ref: 'tags' }] }));
const Tags = db.model('tags', new Schema({ author: Schema.Types.ObjectId }));
const Authors = db.model('authors', new Schema({ name: String }));

return co(function*() {
const anAuthor = new Authors({ name: 'Author1' });
yield anAuthor.save();

const aTag = new Tags({ author: anAuthor.id });
yield aTag.save();

const aBook = new Books({ name: 'Book1', tags: [aTag.id] });
yield aBook.save();

const aggregateOptions = [
{ $match: {
name: { $in: [aBook.name] }
} },
{ $lookup: {
from: 'tags',
localField: 'tags',
foreignField: '_id',
as: 'tags'
} }
];
const books = yield Books.aggregate(aggregateOptions).exec();

const populateOptions = [{
path: 'tags.author',
model: 'authors',
select: '_id name'
}];

const populatedBooks = yield Books.populate(books, populateOptions);
assert.ok(!Array.isArray(populatedBooks[0].tags[0].author));
});
});
});

0 comments on commit c1595d0

Please sign in to comment.