Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented getPopulatedDocs() #9766

Merged
merged 10 commits into from
Jan 9, 2021
20 changes: 20 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
5.11.10 / 2020-01-04
====================
* fix(model): support `populate` option for `insertMany()` as a workaround for mongoose-autopopulate #9720
* perf(schema): avoid creating extra array when initializing array of arrays #9588
* perf(schema): avoid setting `arrayPath` when casting to a non-array, avoid unnecessarily setting atomics #9588
* perf(schema): avoid expensive `String#slice()` call when creating a new array #9588
* fix(queryhelpers): avoid modifying `lean.virtuals` in place #9754
* fix: fall back to legacy treatment for square brackets if square brackets contents aren't a number #9640
* fix(document): make fix for #9396 handle null values more gracefully #9709
* fix(index.d.ts): add missing overloaded function for Document#populate() #9744 [sahasayan](https://github.com/sahasayan)
* fix(index.d.ts): allow Model.create param1 overwrite #9753 [hasezoey](https://github.com/hasezoey)
* fix(index.d.ts): improve autocomplete for query middleware #9752 [3Aahmednaser94](https://github.com/3Aahmednaser94)
* fix(index.d.ts): add missing function for Aggregate#group() #9750 [coro101](https://github.com/coro101)
* fix(index.d.ts): add missing `Aggregate#project()` #9763 [vorticalbox](https://github.com/vorticalbox)
* fix(index.d.ts): allow `null` as an enum value for schematypes #9746
* docs(guide+schema): make schema API docs and guide docs' list of Schema options line up #9749
* docs(documents): add some more details about what the `save()` promise resolves to #9689
* docs(subdocs): add section about subdocument defaults #7291
* chore: run GitHub CI on PRs and update badge #9760 [YC](https://github.com/YC)

5.11.9 / 2020-12-28
===================
* fix(document): keeps atomics when assigning array to filtered array #9651
Expand Down
19 changes: 19 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3128,6 +3128,8 @@ Document.prototype.$__getAllSubdocs = function() {
return subDocs;
};



/*!
* Runs queued functions
*/
Expand Down Expand Up @@ -3897,6 +3899,23 @@ Document.prototype.populate = function populate() {
return this;
};

/* Returns an array of all populated documents associated with the query. */
Document.prototype.$getPopulatedDocs = function $getPopulatedDocs() {
const pathway = (Object.keys(this.$__.populated));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not a fan of this name. Let's just use keys

let result = [];

for (const key of pathway) {
const value = this.get(key);
if (Array.isArray(value)) {
result = result.concat(value);
}
else if (typeof value === 'object' && value !== null) {
result.push(value);
}
}
return result;
};

/**
* Explicitly executes population and returns a promise. Useful for promises integration.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "5.11.9",
"version": "5.11.10",
"author": "Guillermo Rauch <[email protected]>",
"keywords": [
"mongodb",
Expand Down
31 changes: 31 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9834,4 +9834,35 @@ describe('document', function() {
assert.equal(fromDb.arr[0].abc, 'ghi');
});
});

it('supports getting a list of populated docs (gh-9702)', function() {
const Child = db.model('Child', Schema({ name: String }));
const Parent = db.model('Parent', {
children: [{ type: ObjectId, ref: 'Child' }],
child: { type: ObjectId, ref: 'Child' }
});

return co(function*() {
const c = yield Child.create({ name: 'test' });
yield Parent.create({
children: [c._id],
child: c._id
});

const p = yield Parent.findOne().populate('children child');

p.children; // [{ _id: '...', name: 'test' }]

assert.equal(p.$getPopulatedDocs().length, 2);
assert.equal(p.$getPopulatedDocs()[0], p.children[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add a test that populatedDocs[1] is strictly equal to p.child

assert.equal(p.$getPopulatedDocs()[0].name, 'test');
});
});

});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the trailing whitespace