diff --git a/History.md b/History.md index 087bb709e6f..1a046c0130e 100644 --- a/History.md +++ b/History.md @@ -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 diff --git a/index.d.ts b/index.d.ts index 14dc462822d..b0cd3d7ceb6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -384,6 +384,9 @@ declare module 'mongoose' { /** Getter/setter, determines whether the document was removed or not. */ $isDeleted(val?: boolean): boolean; + /** Returns an array of all populated documents associated with the query */ + $getPopulatedDocs(): Document[]; + /** * Returns true if the given path is nullish or only contains empty objects. * Useful for determining whether this subdoc will get stripped out by the diff --git a/lib/document.js b/lib/document.js index b127476a24a..7f0dc3598c4 100644 --- a/lib/document.js +++ b/lib/document.js @@ -3897,6 +3897,21 @@ Document.prototype.populate = function populate() { return this; }; +/* Returns an array of all populated documents associated with the query. */ +Document.prototype.$getPopulatedDocs = function $getPopulatedDocs() { + const keys = (Object.keys(this.$__.populated)); + let result = []; + for (const key of keys) { + const value = this.get(key); + if (Array.isArray(value)) { + result = result.concat(value); + } else if (value instanceof Document) { + result.push(value); + } + } + return result; +}; + /** * Explicitly executes population and returns a promise. Useful for promises integration. * diff --git a/package.json b/package.json index e52fbbd9838..92f6b9efc50 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mongoose", "description": "Mongoose MongoDB ODM", - "version": "5.11.9", + "version": "5.11.10", "author": "Guillermo Rauch ", "keywords": [ "mongodb", diff --git a/test/document.test.js b/test/document.test.js index 6949d55d824..204b36c2b4e 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -9834,4 +9834,30 @@ 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]); + assert.equal(p.$getPopulatedDocs()[0].name, 'test'); + assert.equal(p.$getPopulatedDocs()[1], p.child); + assert.equal(p.$getPopulatedDocs()[1].name, 'test'); + }); + }); +}); \ No newline at end of file