Skip to content

Commit

Permalink
address code review comments and fix a couple of other spots where we…
Browse files Browse the repository at this point in the history
… get _id through getter
  • Loading branch information
vkarpov15 committed Jul 26, 2024
1 parent 7c2cfa8 commit 1ad7531
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
})();

let didPopulate = false;
if (refMatches && val instanceof Document && (!val.$__.wasPopulated || utils.deepEqual(val.$__.wasPopulated.value, val._id))) {
if (refMatches && val instanceof Document && (!val.$__.wasPopulated || utils.deepEqual(val.$__.wasPopulated.value, val._doc._id))) {
const unpopulatedValue = (schema && schema.$isSingleNested) ? schema.cast(val, this) : val._doc._id;
this.$populated(path, unpopulatedValue, { [populateModelSymbol]: val.constructor });
val.$__.wasPopulated = { value: unpopulatedValue };
Expand All @@ -1409,7 +1409,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
schema.options[typeKey][0].ref &&
_isManuallyPopulatedArray(val, schema.options[typeKey][0].ref)) {
popOpts = { [populateModelSymbol]: val[0].constructor };
this.$populated(path, val.map(function(v) { return v._id; }), popOpts);
this.$populated(path, val.map(function(v) { return v._doc._id; }), popOpts);

for (const doc of val) {
doc.$__.wasPopulated = { value: doc._doc._id };
Expand Down Expand Up @@ -1455,7 +1455,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
if (Array.isArray(val) && this.$__.populated[path]) {
for (let i = 0; i < val.length; ++i) {
if (val[i] instanceof Document) {
val.set(i, val[i]._id, true);
val.set(i, val[i]._doc._id, true);
}
}
}
Expand Down Expand Up @@ -1628,7 +1628,7 @@ Document.prototype.$__shouldModify = function(pathToMark, path, options, constru
// if they have the same _id
if (this.$populated(path) &&
val instanceof Document &&
deepEqual(val._id, priorVal)) {
deepEqual(val._doc._id, priorVal)) {
return false;
}

Expand Down
11 changes: 7 additions & 4 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11034,15 +11034,18 @@ describe('model: populate:', function() {
const Pet = db.model('Pet', petSchema);

const ownerId = new mongoose.Types.ObjectId();
const owner = new Owner({
const owner = await Owner.create({
_id: ownerId,
name: 'Alice'
});
await owner.save();
const pet = new Pet({ name: 'Kitty', owner: owner });
await pet.save();
await Pet.create({ name: 'Kitty', owner: owner });

const fromDb = await Pet.findOne({ owner: ownerId }).lean().orFail();
assert.ok(fromDb.owner instanceof mongoose.Types.ObjectId);

const pet1 = new Pet({ name: 'Kitty1', owner: owner });
const pet2 = new Pet({ name: 'Kitty2', owner: owner });
assert.equal(pet1.owner.name, 'Alice');
assert.equal(pet2.owner.name, 'Alice');
});
});

0 comments on commit 1ad7531

Please sign in to comment.