Skip to content

Commit 7dafe5b

Browse files
committed
Merge branch 'master' into 8.1
2 parents 1c82f7d + ec4191e commit 7dafe5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+766
-159
lines changed

.github/workflows/benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
with:
2727
fetch-depth: 0
2828
- name: Setup node
29-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
29+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
3030
with:
3131
node-version: 16
3232

.github/workflows/codeql.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525

2626
# Initializes the CodeQL tools for scanning.
2727
- name: Initialize CodeQL
28-
uses: github/codeql-action/init@v2
28+
uses: github/codeql-action/init@v3
2929
with:
3030
config-file: ./.github/codeql/codeql-config.yml
3131
# Override language selection by uncommenting this and choosing your languages
@@ -44,4 +44,4 @@ jobs:
4444
# make release
4545

4646
- name: Perform CodeQL Analysis
47-
uses: github/codeql-action/analyze@v2
47+
uses: github/codeql-action/analyze@v3

.github/workflows/documentation.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
3232

3333
- name: Setup node
34-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
34+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
3535
with:
3636
node-version: 16
3737

@@ -52,7 +52,7 @@ jobs:
5252
- run: git fetch --depth=1 --tags # download all tags for documentation
5353

5454
- name: Setup node
55-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
55+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
5656
with:
5757
node-version: 16
5858

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: stale
14-
uses: actions/stale@v8
14+
uses: actions/stale@v9
1515
with:
1616
repo-token: ${{ secrets.GITHUB_TOKEN }}
1717
stale-issue-message: 'This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days'

.github/workflows/test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
2626

2727
- name: Setup node
28-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
28+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
2929
with:
3030
node-version: 18
3131

@@ -61,7 +61,7 @@ jobs:
6161
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
6262

6363
- name: Setup node
64-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
64+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
6565
with:
6666
node-version: ${{ matrix.node }}
6767

@@ -80,7 +80,7 @@ jobs:
8080
run: npm run test-coverage
8181
if: matrix.coverage == true
8282
- name: Archive code coverage results
83-
uses: actions/upload-artifact@v3
83+
uses: actions/upload-artifact@v4
8484
if: matrix.coverage == true
8585
with:
8686
name: coverage
@@ -96,7 +96,7 @@ jobs:
9696
steps:
9797
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
9898
- name: Setup node
99-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
99+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
100100
with:
101101
node-version: 16
102102
- name: Load MongoDB binary cache
@@ -124,7 +124,7 @@ jobs:
124124
steps:
125125
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
126126
- name: Setup node
127-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
127+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
128128
with:
129129
node-version: 16
130130
- run: npm install

.github/workflows/tidelift-alignment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Checkout
1818
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
1919
- name: Setup node
20-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
20+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
2121
with:
2222
node-version: 16
2323
- name: Alignment

.github/workflows/tsd.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
2424

2525
- name: Setup node
26-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
26+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
2727
with:
2828
node-version: 18
2929

@@ -41,7 +41,7 @@ jobs:
4141
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
4242

4343
- name: Setup node
44-
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
44+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
4545
with:
4646
node-version: 14
4747

CHANGELOG.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
8.0.4 / 2024-01-08
2+
==================
3+
* fix(update): set CastError path to full path if casting update fails #14161 #14114
4+
* fix: cast error when there is an elemMatch in the and clause #14171 [tosaka-n](https://github.com/tosaka-n)
5+
* fix: allow defining index on base model that applies to all discriminators #14176 [peplin](https://github.com/peplin)
6+
* fix(model): deep clone bulkWrite() updateOne arguments to avoid mutating documents in update #14197 #14164
7+
* fix(populate): handle deselecting _id with array of fields in populate() #14242 #14231
8+
* types(model+query): use stricter typings for updateX(), replaceOne(),deleteX() Model functions #14228 #14204
9+
* types: fix return types for findByIdAndDelete overrides #14196 #14190
10+
* types(schema): add missing omit() method #14235 [amitbeck](https://github.com/amitbeck)
11+
* types(model): add missing strict property to bulkWrite() top level options #14239
12+
* docs(compatibility): add note that Mongoose 5.13 is fully compatible with MongoDB server 5 #14230 #14149
13+
* docs: add shared schemas guide #14211
14+
* docs: update TLS/SSL guide for Mongoose v8 - MongoDB v6 driver deprecations #14170 [andylwelch](https://github.com/andylwelch)
15+
* docs: update findOneAndUpdate tutorial to use includeResultMetadata #14208 #14207
16+
* docs: clarify disabling _id on subdocs #14195 #14194
17+
18+
7.6.8 / 2024-01-08
19+
==================
20+
* perf(schema): remove unnecessary lookahead in numeric subpath check
21+
* fix(discriminator): handle reusing schema with embedded discriminators defined using Schema.prototype.discriminator #14202 #14162
22+
* fix(ChangeStream): avoid suppressing errors in closed change stream #14206 #14177
23+
24+
6.12.5 / 2024-01-03
25+
===================
26+
* perf(schema): remove unnecessary lookahead in numeric subpath check
27+
* fix(document): allow setting nested path to null #14226
28+
* fix(document): avoid flattening dotted paths in mixed path underneath nested path #14198 #14178
29+
* fix: add ignoreAtomics option to isModified() for better backwards compatibility with Mongoose 5 #14213
30+
31+
6.12.4 / 2023-12-27
32+
===================
33+
* fix: upgrade mongodb driver -> 4.17.2
34+
* fix(document): avoid treating nested projection as inclusive when applying defaults #14173 #14115
35+
* fix: account for null values when assigning isNew property #14172 #13883
36+
137
8.0.3 / 2023-12-07
238
==================
339
* fix(schema): avoid creating unnecessary clone of schematype in nested array so nested document arrays use correct constructor #14128 #14101

docs/compatibility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Below are the [semver](http://semver.org/) ranges representing which versions of
2020
| :------------: | :-------------------------------------: |
2121
| `7.x` | `^7.4.0 \| ^8.0.0` |
2222
| `6.x` | `^6.5.0 \| ^7.0.0 \| ^8.0.0` |
23-
| `5.x` | `^6.0.0 \| ^7.0.0 \| ^8.0.0` |
23+
| `5.x` | `^5.13.0` | `^6.0.0 \| ^7.0.0 \| ^8.0.0`|
2424
| `4.4.x` | `^5.10.0 \| ^6.0.0 \| ^7.0.0 \| ^8.0.0` |
2525
| `4.2.x` | `^5.7.0 \| ^6.0.0 \| ^7.0.0 \| ^8.0.0` |
2626
| `4.0.x` | `^5.2.0 \| ^6.0.0 \| ^7.0.0 \| ^8.0.0` |

docs/guide.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ const schema = new Schema();
109109
schema.path('_id'); // ObjectId { ... }
110110
```
111111

112-
When you create a new document with the automatically added
113-
`_id` property, Mongoose creates a new [`_id` of type ObjectId](https://masteringjs.io/tutorials/mongoose/objectid)
114-
to your document.
112+
When you create a new document with the automatically added `_id` property, Mongoose creates a new [`_id` of type ObjectId](https://masteringjs.io/tutorials/mongoose/objectid) to your document.
115113

116114
```javascript
117115
const Model = mongoose.model('Test', schema);
@@ -120,13 +118,13 @@ const doc = new Model();
120118
doc._id instanceof mongoose.Types.ObjectId; // true
121119
```
122120

123-
You can also overwrite Mongoose's default `_id` with your
124-
own `_id`. Just be careful: Mongoose will refuse to save a
125-
document that doesn't have an `_id`, so you're responsible
126-
for setting `_id` if you define your own `_id` path.
121+
You can also overwrite Mongoose's default `_id` with your own `_id`.
122+
Just be careful: Mongoose will refuse to save a top-level document that doesn't have an `_id`, so you're responsible for setting `_id` if you define your own `_id` path.
127123

128124
```javascript
129-
const schema = new Schema({ _id: Number });
125+
const schema = new Schema({
126+
_id: Number // <-- overwrite Mongoose's default `_id`
127+
});
130128
const Model = mongoose.model('Test', schema);
131129

132130
const doc = new Model();
@@ -136,6 +134,37 @@ doc._id = 1;
136134
await doc.save(); // works
137135
```
138136

137+
Mongoose also adds an `_id` property to subdocuments.
138+
You can disable the `_id` property on your subdocuments as follows.
139+
Mongoose does allow saving subdocuments without an `_id` property.
140+
141+
```javascript
142+
const nestedSchema = new Schema(
143+
{ name: String },
144+
{ _id: false } // <-- disable `_id`
145+
);
146+
const schema = new Schema({
147+
subdoc: nestedSchema,
148+
docArray: [nestedSchema]
149+
});
150+
const Test = mongoose.model('Test', schema);
151+
152+
// Neither `subdoc` nor `docArray.0` will have an `_id`
153+
await Test.create({
154+
subdoc: { name: 'test 1' },
155+
docArray: [{ name: 'test 2' }]
156+
});
157+
```
158+
159+
Alternatively, you can disable `_id` using the following syntax:
160+
161+
```javascript
162+
const nestedSchema = new Schema({
163+
_id: false, // <-- disable _id
164+
name: String
165+
});
166+
```
167+
139168
<h2 id="methods"><a href="#methods">Instance methods</a></h2>
140169

141170
Instances of `Models` are [documents](documents.html). Documents have

docs/guides.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ integrating Mongoose with external tools and frameworks.
2929
* [Custom Casting For Built-in Types](tutorials/custom-casting.html)
3030
* [Custom SchemaTypes](customschematypes.html)
3131
* [Advanced Schemas](advanced_schemas.html)
32+
* [Sharing Schemas Between Mongoose Projects](shared-schemas.html)
3233

3334
## Integrations
3435

@@ -41,7 +42,7 @@ integrating Mongoose with external tools and frameworks.
4142
* [Transactions](transactions.html)
4243
* [MongoDB Driver Deprecation Warnings](deprecations.html)
4344
* [Testing with Jest](jest.html)
44-
* [SSL Connections](tutorials/ssl.html)
45+
* [TLS/SSL Connections](tutorials/ssl.html)
4546
* [MongoDB Client Side Field Level Encryption](field-level-encryption.html)
4647

4748
## Other Guides

docs/layout.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ html(lang='en')
6262
- if ([`${versions.versionedPath}/docs/connections`, `${versions.versionedPath}/docs/tutorials/ssl`].some(path => outputUrl.startsWith(path)))
6363
ul.pure-menu-list
6464
li.pure-menu-item.sub-item
65-
a.pure-menu-link(href=`${versions.versionedPath}/docs/tutorials/ssl.html`, class=outputUrl === `${versions.versionedPath}/docs/tutorials/ssl.html` ? 'selected' : '') SSL Connections
65+
a.pure-menu-link(href=`${versions.versionedPath}/docs/tutorials/ssl.html`, class=outputUrl === `${versions.versionedPath}/docs/tutorials/ssl.html` ? 'selected' : '') TLS/SSL Connections
6666
li.pure-menu-item.sub-item
6767
a.pure-menu-link(href=`${versions.versionedPath}/docs/models.html`, class=outputUrl === `${versions.versionedPath}/docs/models.html` ? 'selected' : '') Models
6868
- if ([`${versions.versionedPath}/docs/models`, `${versions.versionedPath}/docs/change-streams`].some(path => outputUrl.startsWith(path)))

docs/migrating_to_8.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ There's a few noteable changes in MongoDB Node driver v6 that affect Mongoose:
7171

7272
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.
7373

74+
1. Deprecated SSL options have been removed
75+
76+
* `sslCA` -> `tlsCAFile`
77+
* `sslCRL` -> `tlsCRLFile`
78+
* `sslCert` -> `tlsCertificateKeyFile`
79+
* `sslKey` -> `tlsCertificateKeyFile`
80+
* `sslPass` -> `tlsCertificateKeyFilePassword`
81+
* `sslValidate` -> `tlsAllowInvalidCertificates`
82+
* `tlsCertificateFile` -> `tlsCertificateKeyFile`
83+
7484
<h2 id="removed-findoneandremove"><a href="#removed-findoneandremove">Removed <code>findOneAndRemove()</code></a></h2>
7585

7686
In Mongoose 7, `findOneAndRemove()` was an alias for `findOneAndDelete()` that Mongoose supported for backwards compatibility.

docs/shared-schemas.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Sharing Schemas Between Mongoose Projects
2+
3+
In larger organizations, it is common to have a project that contains schemas which are shared between multiple projects.
4+
For example, suppose your company has an `@initech/shared-schemas` private npm package, and `npm list` looks like the following:
5+
6+
```sh
7+
@initech/web-app1@1.0.0
8+
├── @initech/shared-schemas@1.0.0
9+
├── mongoose@8.0.1
10+
```
11+
12+
In the above output, `@initech/web-app1` is a *client project* and `@initech/shared-schemas` is the *shared library*.
13+
14+
## Put Mongoose as a Peer Dependency
15+
16+
First, and most importantly, we recommend that `@initech/shared-schemas` list Mongoose in [your shared-schema's `peerDependencies`](https://docs.npmjs.com/cli/v10/configuring-npm/package-json#peerdependencies), **not** as a top-level dependency.
17+
For example, `@initech/shared-schemas`'s `package.json` should look like the following.
18+
19+
```javascript
20+
{
21+
"name": "@initech/shared-schemas",
22+
"peerDependencies": {
23+
"mongoose": "8.x"
24+
}
25+
}
26+
```
27+
28+
We recommend this approach for the following reasons:
29+
30+
1. Easier to upgrade. For example, suppose `@initech/shared-schemas` has a dependency on Mongoose 8, and `@initech/web-app1` works fine with Mongoose 8; but `@initech/web-app2` cannot upgrade from Mongoose 7. Peer dependencies makes it easier for the projects that rely on your shared schemas to determine which version of Mongoose they want, without risking having conflicting versions of the Mongoose module.
31+
2. Reduce risk of Mongoose module duplicates. Using Mongoose schemas and models from one version of Mongoose with another version is not supported.
32+
33+
## Export Schemas, Not Models
34+
35+
We recommend that `@initech/shared-schemas` export Mongoose schemas, **not** models.
36+
This approach is more flexible and allows client projects to instantiate models using their preferred patterns.
37+
In particular, if `@initech/shared-schemas` exports a model that is registered using `mongoose.model()`, there is no way to transfer that model to a different connection.
38+
39+
```javascript
40+
// `userSchema.js` in `@initech/shared-schemas`
41+
const userSchema = new mongoose.Schema({ name: String });
42+
43+
// Do this:
44+
module.exports = userSchema;
45+
46+
// Not this:
47+
module.exports = mongoose.model('User', userSchema);
48+
```
49+
50+
## Workaround: Export a POJO
51+
52+
Sometimes, existing shared libraries don't follow the above best practices.
53+
If you find yourself with a shared library that depends on an old version of Mongoose, a helpful workaround is to export a [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) rather than a schema or model.
54+
This will remove any conflicts between the shared library's version of Mongoose and the client project's version of Mongoose.
55+
56+
```javascript
57+
// Replace this:
58+
module.exports = new mongoose.Schema({ name: String });
59+
60+
// With this:
61+
module.exports = { name: String };
62+
```
63+
64+
And update your client project to do the following:
65+
66+
```javascript
67+
// Replace this:
68+
const { userSchema } = require('@initech/shared-schemas');
69+
70+
// With this:
71+
const { userSchemaDefinition } = require('@initech/shared-schemas');
72+
const userSchema = new mongoose.Schema(userSchemaDefinition);
73+
```

docs/source/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ docs['docs/compatibility.md'] = {
8181
guide: true,
8282
markdown: true
8383
};
84+
docs['docs/shared-schemas.md'] = { guide: true, title: 'Sharing Schemas Between Mongoose Projects', markdown: true };
8485
docs['docs/field-level-encryption.md'] = { guide: true, title: 'Field Level Encryption', markdown: true };
8586
docs['docs/timestamps.md'] = { title: 'Mongoose Timestamps', markdown: true };
8687
docs['docs/search.pug'] = { title: 'Search' };

docs/tutorials/findoneandupdate.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ However, there are some cases where you need to use [`findOneAndUpdate()`](https
66
* [Getting Started](#getting-started)
77
* [Atomic Updates](#atomic-updates)
88
* [Upsert](#upsert)
9-
* [The `rawResult` Option](#raw-result)
9+
* [The `includeResultMetadata` Option](#includeresultmetadata)
1010

1111
## Getting Started
1212

@@ -51,17 +51,17 @@ Using the `upsert` option, you can use `findOneAndUpdate()` as a find-and-[upser
5151
[require:Tutorial.*findOneAndUpdate.*upsert]
5252
```
5353

54-
<h2 id="raw-result">The `rawResult` Option</h2>
54+
<h2 id="includeresultmetadata">The <code>includeResultMetadata</code> Option<h2 id="rawresult"></h2></h2>
5555

5656
Mongoose transforms the result of `findOneAndUpdate()` by default: it
5757
returns the updated document. That makes it difficult to check whether
5858
a document was upserted or not. In order to get the updated document
5959
and check whether MongoDB upserted a new document in the same operation,
60-
you can set the `rawResult` flag to make Mongoose return the raw result
60+
you can set the `includeResultMetadata` flag to make Mongoose return the raw result
6161
from MongoDB.
6262

6363
```acquit
64-
[require:Tutorial.*findOneAndUpdate.*rawResult$]
64+
[require:Tutorial.*findOneAndUpdate.*includeResultMetadata$]
6565
```
6666

6767
Here's what the `res` object from the above example looks like:

0 commit comments

Comments
 (0)