Skip to content

Commit eaa1d2d

Browse files
committed
Merge branch 'master' into 8.1
2 parents 33e3898 + c463e45 commit eaa1d2d

22 files changed

+343
-141
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
8.0.2 / 2023-11-28
2+
==================
3+
* fix(populate): set populated docs in correct order when populating virtual underneath doc array with justOne #14105
4+
* fix(populate): fix curPath to update appropriately #14099 #14098 [csy1204](https://github.com/csy1204)
5+
* types: make property names show up in intellisense for UpdateQuery #14123 #14090
6+
* types(document): correct return type for doc.deleteOne() re: Mongoose 8 breaking change #14110 #14081
7+
* types: correct types for when includeResultMetadata: true is set #14078
8+
* types(models): allow specifying timestamps as inline option for bulkWrite() operations #14112 #14072
9+
* docs: fix rendering of 7.x server compatibility #14086 [laupow](https://github.com/laupow)
10+
* docs(source/api): fix "index.js" -> "mongoose.js" rename #14125
11+
* docs(README): update breaking change version #14126
12+
13+
7.6.6 / 2023-11-27
14+
==================
15+
* perf: avoid double-running setter logic when calling `push()` #14120 #11380
16+
* fix(populate): set populated docs in correct order when populating virtual underneath doc array with justOne #14105 #14018
17+
* fix: bump mongodb driver -> 5.9.1 #14084 #13829 [lorand-horvath](https://github.com/lorand-horvath)
18+
* types: allow defining document array using [{ prop: String }] syntax #14095 #13424
19+
* types: correct types for when includeResultMetadata: true is set #14078 #13987 [prathamVaidya](https://github.com/prathamVaidya)
20+
* types(query): base filters and projections off of RawDocType instead of DocType so autocomplete doesn't show populate #14118 #14077
21+
* types: make property names show up in intellisense for UpdateQuery #14123 #14090
22+
* types(model): support calling Model.validate() with pathsToSkip option #14088 #14003
23+
* docs: remove "DEPRECATED" warning mistakenly added to read() tags param #13980
24+
125
8.0.1 / 2023-11-15
226
==================
327
* fix: retain key order with aliases when creating indexes with alias #14042 [meabed](https://github.com/meabed)

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ Anyone can file an expense. If the expense makes sense for the development of th
8080
### Contributors
8181

8282
Thank you to all the people who have already contributed to mongoose!
83-
<a href="https://github.com/Automattic/mongoose/graphs/contributors"><img src="https://opencollective.com/mongoose/contributors.svg?width=890" /></a>
83+
<a href="https://github.com/Automattic/mongoose/graphs/contributors"><img src="https://opencollective.com/mongoose/contributors.svg?width=890" alt="Mongoose contributors" /></a>
8484

8585
### Backers
8686

8787
Thank you to all our backers! [[Become a backer](https://opencollective.com/mongoose#backer)]
8888

89-
<a href="https://opencollective.com/mongoose#backers" target="_blank"><img src="https://opencollective.com/mongoose/backers.svg?width=890"></a>
89+
<a href="https://opencollective.com/mongoose#backers" target="_blank"><img src="https://opencollective.com/mongoose/backers.svg?width=890" alt="Mongoose backers"></a>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed
1313

1414
The official documentation website is [mongoosejs.com](http://mongoosejs.com/).
1515

16-
Mongoose 7.0.0 was released on February 27, 2023. You can find more details on [backwards breaking changes in 7.0.0 on our docs site](https://mongoosejs.com/docs/migrating_to_7.html).
16+
Mongoose 8.0.0 was released on October 31, 2023. You can find more details on [backwards breaking changes in 8.0.0 on our docs site](https://mongoosejs.com/docs/migrating_to_8.html).
1717

1818
## Support
1919

docs/further_reading.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ on this page to make sure it is high quality.
3535
## Video Courses
3636

3737
<a href="//pluralsight.pxf.io/c/1321469/432943/7490" class="pluralsight-link">
38-
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="" width="180" height="95"/>
38+
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="" width="180" height="95" alt="API Design in Node.js Featuring Express & Mongo" />
3939
</a>
40-
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" />
40+
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" alt="Pixel" />
4141

4242
<a class="pluralsight-title" href="https://pluralsight.pxf.io/c/1321469/424552/7490?u=https%3A%2F%2Fapp.pluralsight.com%2Flibrary%2Fcourses%2Fapi-design-nodejs-express-mongo%2Ftable-of-contents">
4343
API Design in Node.js Featuring Express & Mongo
@@ -50,7 +50,7 @@ a serial entrepreneur and former instructor at [Hack Reactor](https://www.hackre
5050
<hr/>
5151

5252
<a href="https://thinkster.io/tutorials/node-json-api" class="pluralsight-link" style="padding:20px">
53-
<img src="https://pbs.twimg.com/profile_images/819400302864412676/6x2g2XtJ_400x400.jpg" alt="" style="width: 140px" />
53+
<img src="https://pbs.twimg.com/profile_images/819400302864412676/6x2g2XtJ_400x400.jpg" alt="Building a Production Ready Node.js JSON API" style="width: 140px" />
5454
</a>
5555

5656
<a class="pluralsight-title" href="https://thinkster.io/tutorials/node-json-api">
@@ -67,9 +67,9 @@ RESTful API with Express and Mongoose from scratch.
6767
<hr/>
6868

6969
<a href="//pluralsight.pxf.io/c/1321469/432943/7490" class="pluralsight-link">
70-
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="" width="180" height="95"/>
70+
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="Introduction to Mongoose for Node.js and MongoDB" width="180" height="95"/>
7171
</a>
72-
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" />
72+
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" alt="Pixel" />
7373

7474
<a class="pluralsight-title" href="https://pluralsight.pxf.io/c/1321469/424552/7490?u=https%3A%2F%2Fapp.pluralsight.com%2Flibrary%2Fcourses%2Fmongoose-for-nodejs-mongodb%2Ftable-of-contents">
7575
Introduction to Mongoose for Node.js and MongoDB
@@ -83,9 +83,9 @@ resource for beginners looking to get started.
8383
<hr/>
8484

8585
<a href="//pluralsight.pxf.io/c/1321469/432943/7490" class="pluralsight-link">
86-
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="" width="180" height="95"/>
86+
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="RESTful Web Services with Node.js and Express" width="180" height="95"/>
8787
</a>
88-
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" />
88+
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" alt="Pixel" />
8989

9090
<a class="pluralsight-title" href="https://pluralsight.pxf.io/c/1321469/424552/7490?u=https%3A%2F%2Fapp.pluralsight.com%2Flibrary%2Fcourses%2Fnode-js-express-rest-web-services%2Ftable-of-contents">
9191
RESTful Web Services with Node.js and Express
@@ -99,9 +99,9 @@ between *API Design* and *Introduction to Mongoose*. It focuses more on
9999
<hr/>
100100

101101
<a href="//pluralsight.pxf.io/c/1321469/432943/7490" class="pluralsight-link">
102-
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="" width="180" height="95"/>
102+
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="Building Business Applications with Vue.js and MongoDB" width="180" height="95"/>
103103
</a>
104-
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" />
104+
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" alt="Pixel" />
105105

106106
<a class="pluralsight-title" href="https://pluralsight.pxf.io/c/1321469/424552/7490?u=https%3A%2F%2Fapp.pluralsight.com%2Flibrary%2Fcourses%2Fbuilding-business-applications-vuejs%2Ftable-of-contents">
107107
Building Business Applications with Vue.js and MongoDB
@@ -116,9 +116,9 @@ learn about frontend dev.
116116
<hr/>
117117

118118
<a href="//pluralsight.pxf.io/c/1321469/432943/7490" class="pluralsight-link">
119-
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="" width="180" height="95"/>
119+
<img src="//a.impactradius-go.com/display-ad/7490-432943" alt="Moving Forward with Mongoose.js" width="180" height="95"/>
120120
</a>
121-
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" />
121+
<img height="0" width="0" src="//pluralsight.pxf.io/i/1321469/432943/7490" style="position:absolute;visibility:hidden;" alt="Pixel" />
122122

123123
<a class="pluralsight-title" href="https://pluralsight.pxf.io/c/1321469/424552/7490?u=https%3A%2F%2Fapp.pluralsight.com%2Flibrary%2Fcourses%2Fmongoosejs-moving-forward%2Ftable-of-contents">
124124
Moving Forward with Mongoose.js
@@ -133,7 +133,7 @@ so you can upgrade with confidence.
133133
## Books
134134

135135
<a class="pluralsight-link" href="http://bit.ly/mongoose-book-2013">
136-
<img src="https://s3.amazonaws.com/codebarbarian-images/mongoose.jpg" style="width: 180px">
136+
<img src="https://s3.amazonaws.com/codebarbarian-images/mongoose.jpg" style="width: 180px" alt="Mongoose for Application Development">
137137
</a>
138138

139139
<a class="pluralsight-title" href="http://bit.ly/mongoose-book-2013">
@@ -148,7 +148,7 @@ powerful. Just sub out callbacks for promises.
148148
<hr/>
149149

150150
<a class="pluralsight-link" href="https://bit.ly/mongodb-schema-design">
151-
<img src="https://s3.amazonaws.com/codebarbarian-images/schemadesign.jpg" style="width: 180px">
151+
<img src="https://s3.amazonaws.com/codebarbarian-images/schemadesign.jpg" style="width: 180px" alt="The Little MongoDB Schema Design Book">
152152
</a>
153153

154154
<a class="pluralsight-title" href="https://bit.ly/mongodb-schema-design">
@@ -170,7 +170,7 @@ the book for you.
170170
## Blog Posts
171171

172172
<a class="pluralsight-link" style="padding: 20px" href="https://medium.freecodecamp.org/introduction-to-mongoose-for-mongodb-d2a7aa593c57">
173-
<img src="https://cdn-images-1.medium.com/max/800/1*uTZXsVta4TwghNobMkZeZg.png" style="width: 140px">
173+
<img src="https://cdn-images-1.medium.com/max/800/1*uTZXsVta4TwghNobMkZeZg.png" style="width: 140px" alt="Introduction to Mongoose for MongoDB">
174174
</a>
175175

176176
<a class="pluralsight-title" href="https://medium.freecodecamp.org/introduction-to-mongoose-for-mongodb-d2a7aa593c57">
@@ -184,7 +184,7 @@ concepts in SQL.
184184
<hr/>
185185

186186
<a class="pluralsight-link" style="padding: 20px" href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose">
187-
<img src="https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png" style="width: 140px">
187+
<img src="https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png" style="width: 140px" alt="Express Tutorial Part 3: Using a Database (with Mongoose)">
188188
</a>
189189

190190
<a class="pluralsight-title" href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose">

docs/geojson.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ the city of Denver, Colorado. It then queries for all documents within
115115
a polygon representing the state of Colorado using
116116
[the MongoDB `$geoWithin` operator](https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/).
117117

118-
<img src="https://i.imgur.com/i32pWnC.png">
118+
<img src="https://i.imgur.com/i32pWnC.png" alt="Colorado GeoJSON Polygon">
119119

120120
```acquit
121121
[require:geojson.*driver query]

docs/jest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ Want to learn how to test Mongoose apps correctly? The
8686
course on Pluralsight has a great section on testing Mongoose apps with [Mocha](http://npmjs.com/package/mocha).
8787

8888
<a href="https://pluralsight.pxf.io/c/1321469/424552/7490?u=https%3A%2F%2Fapp.pluralsight.com%2Flibrary%2Fcourses%2Fnode-js-express-rest-web-services%2Ftable-of-contents">
89-
<img src="https://i.imgur.com/KouuaAZ.png">
89+
<img src="https://i.imgur.com/KouuaAZ.png" alt="RESTful Web Services with Node.js and Express">
9090
</a>

docs/migrating_to_8.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
}
77
</style>
88

9-
There are several backwards-breaking changes
10-
you should be aware of when migrating from Mongoose 7.x to Mongoose 8.x.
9+
There are several backwards-breaking changes you should be aware of when migrating from Mongoose 7.x to Mongoose 8.x.
1110

1211
If you're still on Mongoose 6.x or earlier, please read the [Mongoose 6.x to 7.x migration guide](migrating_to_7.html) and upgrade to Mongoose 7.x first before upgrading to Mongoose 8.
1312

13+
We also recommend reviewing the [MongoDB Node.js driver's release notes for v6.0.0](https://github.com/mongodb/node-mongodb-native/releases/tag/v6.0.0) before upgrading to Mongoose 8.
14+
1415
* [Removed `rawResult` option for `findOneAndUpdate()`](#removed-rawresult-option-for-findoneandupdate)
1516
* [`Document.prototype.deleteOne()` now returns a query](#document-prototype-deleteone-now-returns-a-query)
1617
* [MongoDB Node Driver 6.0](#mongodb-node-driver-6)
@@ -65,7 +66,7 @@ const res = await q;
6566

6667
<h2 id="mongodb-node-driver-6"><a href="#mongodb-node-driver-6">MongoDB Node Driver 6</a></h2>
6768

68-
Mongoose 8 uses [v6.x of the MongoDB Node driver](https://github.com/mongodb/node-mongodb-native/blob/main/HISTORY.md#600-2023-08-28).
69+
Mongoose 8 uses [v6.x of the MongoDB Node driver](https://github.com/mongodb/node-mongodb-native/releases/tag/v6.0.0).
6970
There's a few noteable changes in MongoDB Node driver v6 that affect Mongoose:
7071

7172
1. The `ObjectId` constructor no longer accepts strings of length 12. In Mongoose 7, `new mongoose.Types.ObjectId('12charstring')` was perfectly valid. In Mongoose 8, `new mongoose.Types.ObjectId('12charstring')` throws an error.

docs/populate.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ the `Story` model.
4646
<li><a href="#populate_multiple_documents">Populating multiple existing documents</a></li>
4747
<li><a href="#deep-populate">Populating across multiple levels</a></li>
4848
<li><a href="#cross-db-populate">Populating across Databases</a></li>
49-
<li><a href="#dynamic-ref">Dynamic References via <code>refPath</code></a></li>
49+
<li><a href="#dynamic-refpath">Dynamic References via <code>refPath</code></a></li>
50+
<li><a href="#dynamic-ref">Dynamic References via <code>ref</code></a></li>
5051
<li><a href="#populate-virtuals">Populate Virtuals</a></li>
5152
<li><a href="#count">Populate Virtuals: The Count Option</a></li>
5253
<li><a href="#match">Populate Virtuals: The Match Option</a></li>
@@ -469,7 +470,7 @@ const events = await Event.
469470
populate({ path: 'conversation', model: Conversation });
470471
```
471472

472-
<h2 id="dynamic-ref"><a href="#dynamic-ref">Dynamic References via <code>refPath</code></a></h2>
473+
<h2 id="dynamic-refpath"><a href="#dynamic-refpath">Dynamic References via <code>refPath</code></a></h2>
473474

474475
Mongoose can also populate from multiple collections based on the value
475476
of a property in the document. Let's say you're building a schema for
@@ -561,6 +562,53 @@ also need an extra `populate()` call for every property, unless you use
561562
Using `refPath` means you only need 2 schema paths and one `populate()` call
562563
regardless of how many models your `commentSchema` can point to.
563564

565+
You could also assign a function to `refPath`, which means Mongoose selects a refPath depending on a value on the document being populated.
566+
567+
```javascript
568+
const commentSchema = new Schema({
569+
body: { type: String, required: true },
570+
commentType: {
571+
type: String,
572+
enum: ['comment', 'review']
573+
},
574+
entityId: {
575+
type: Schema.Types.ObjectId,
576+
required: true,
577+
refPath: function () {
578+
return this.commentType === 'review' ? this.reviewEntityModel : this.commentEntityModel; // 'this' refers to the document being populated
579+
}
580+
},
581+
commentEntityModel: {
582+
type: String,
583+
required: true,
584+
enum: ['BlogPost', 'Review']
585+
},
586+
reviewEntityModel: {
587+
type: String,
588+
required: true,
589+
enum: ['Vendor', 'Product']
590+
}
591+
});
592+
```
593+
594+
<h2 id="dynamic-ref"><a href="#dynamic-ref">Dynamic References via <code>ref</code></a></h2>
595+
596+
Just like `refPath`, `ref` can also be assigned a function.
597+
598+
```javascript
599+
const commentSchema = new Schema({
600+
body: { type: String, required: true },
601+
verifiedBuyer: Boolean
602+
doc: {
603+
type: Schema.Types.ObjectId,
604+
required: true,
605+
ref: function() {
606+
return this.verifiedBuyer ? 'Product' : 'BlogPost'; // 'this' refers to the document being populated
607+
}
608+
},
609+
});
610+
```
611+
564612
<h2 id="populate-virtuals"><a href="#populate-virtuals">Populate Virtuals</a></h2>
565613

566614
So far you've only populated based on the `_id` field.

docs/promises.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ better stack traces.
6060
</i>
6161
<br><br>
6262
<a href="http://asyncawait.net/?utm_source=mongoosejs&utm_campaign=promises" style="margin-left: 100px">
63-
<img src="/docs/images/asyncawait.png" style="width: 650px" />
63+
<img src="/docs/images/asyncawait.png" style="width: 650px" alt="Mastering Async/Await" />
6464
</a>

docs/source/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const fs = require('fs');
99
const md = require('marked');
1010

1111
const files = [
12-
'lib/index.js',
12+
'lib/mongoose.js',
1313
'lib/schema.js',
1414
'lib/connection.js',
1515
'lib/document.js',

lib/aggregate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ Aggregate.prototype.unionWith = function(options) {
665665
* await Model.aggregate(pipeline).read('primaryPreferred');
666666
*
667667
* @param {String|ReadPreference} pref one of the listed preference options or their aliases
668-
* @param {Array} [tags] optional tags for this query. DEPRECATED
668+
* @param {Array} [tags] optional tags for this query.
669669
* @return {Aggregate} this
670670
* @api public
671671
* @see mongodb https://www.mongodb.com/docs/manual/applications/replication/#read-preference

lib/schema.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,15 +1122,14 @@ Schema.prototype.path = function(path, obj) {
11221122
if (_schemaType.$isMongooseDocumentArray) {
11231123
_schemaType.$embeddedSchemaType._arrayPath = arrayPath;
11241124
_schemaType.$embeddedSchemaType._arrayParentPath = path;
1125-
_schemaType = _schemaType.$embeddedSchemaType.clone();
1125+
_schemaType = _schemaType.$embeddedSchemaType;
11261126
} else {
11271127
_schemaType.caster._arrayPath = arrayPath;
11281128
_schemaType.caster._arrayParentPath = path;
1129-
_schemaType = _schemaType.caster.clone();
1129+
_schemaType = _schemaType.caster;
11301130
}
11311131

1132-
_schemaType.path = arrayPath;
1133-
toAdd.push(_schemaType);
1132+
this.subpaths[arrayPath] = _schemaType;
11341133
}
11351134

11361135
for (const _schemaType of toAdd) {

lib/stateMachine.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ StateMachine.ctor = function() {
6565
*/
6666

6767
StateMachine.prototype._changeState = function _changeState(path, nextState) {
68-
const prevBucket = this.states[this.paths[path]];
68+
const prevState = this.paths[path];
69+
if (prevState === nextState) {
70+
return;
71+
}
72+
const prevBucket = this.states[prevState];
6973
if (prevBucket) delete prevBucket[path];
7074

7175
this.paths[path] = nextState;

0 commit comments

Comments
 (0)