Skip to content

Commit

Permalink
Merge branch '7.x' into mms9
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed Apr 19, 2024
2 parents 28ca4f2 + c97c060 commit a4c9e10
Show file tree
Hide file tree
Showing 83 changed files with 2,290 additions and 470 deletions.
4 changes: 4 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ reporter: spec # better to identify failing / slow tests than "dot"
ui: bdd # explicitly setting, even though it is mocha default
require:
- test/mocha-fixtures.js
extension:
- test.js
watch-files:
- test/**/*.js
11 changes: 10 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ webpack.base.config.js
notes.md
list.out

eslintrc.json
# config files
lgtm.yml
.mocharc.yml
.eslintrc.js
.markdownlint-cli2.cjs
tsconfig.json

# scripts
scripts/
tools/
139 changes: 139 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,142 @@
7.6.11 / 2024-04-11
===================
* fix(populate): avoid match function filtering out null values in populate result #14518
* fix(schema): support setting discriminator options in Schema.prototype.discriminator() #14493 #14448
* fix(schema): deduplicate idGetter so creating multiple models with same schema doesn't result in multiple id getters #14492 #14457

6.12.8 / 2024-04-10
===================
* fix(document): handle virtuals that are stored as objects but getter returns string with toJSON #14468 #14446
* fix(schematype): consistently set wasPopulated to object with `value` property rather than boolean #14418
* docs(model): add extra note about lean option for insertMany() skipping casting #14415 #14376

7.6.10 / 2024-03-13
===================
* docs(model): add extra note about lean option for insertMany() skipping casting #14415
* docs(mongoose): add options.overwriteModel details to mongoose.model() docs #14422

6.12.7 / 2024-03-01
===================
* perf(model): make insertMany() lean option skip hydrating Mongoose docs #14376 #14372
* perf(document+schema): small optimizations to make init() faster #14383 #14113
* fix(connection): don't modify passed options object to `openUri()` #14370 #13376 #13335
* fix(ChangeStream): bubble up resumeTokenChanged changeStream event #14355 #14349 [3150](https://github.com/3150)

7.6.9 / 2024-02-26
==================
* fix(document): handle embedded recursive discriminators on nested path defined using Schema.prototype.discriminator #14256 #14245
* types(model): correct return type for findByIdAndDelete() #14233 #14190
* docs(connections): add note about using asPromise() with createConnection() for error handling #14364 #14266
* docs(model+query+findoneandupdate): add more details about overwriteDiscriminatorKey option to docs #14264 #14246

6.12.6 / 2024-01-22
===================
* fix(collection): correctly handle buffer timeouts with find() #14277
* fix(document): allow calling push() with different $position arguments #14254

7.6.8 / 2024-01-08
==================
* perf(schema): remove unnecessary lookahead in numeric subpath check
* fix(discriminator): handle reusing schema with embedded discriminators defined using Schema.prototype.discriminator #14202 #14162
* fix(ChangeStream): avoid suppressing errors in closed change stream #14206 #14177

6.12.5 / 2024-01-03
===================
* perf(schema): remove unnecessary lookahead in numeric subpath check
* fix(document): allow setting nested path to null #14226
* fix(document): avoid flattening dotted paths in mixed path underneath nested path #14198 #14178
* fix: add ignoreAtomics option to isModified() for better backwards compatibility with Mongoose 5 #14213

6.12.4 / 2023-12-27
===================
* fix: upgrade mongodb driver -> 4.17.2
* fix(document): avoid treating nested projection as inclusive when applying defaults #14173 #14115
* fix: account for null values when assigning isNew property #14172 #13883

7.6.7 / 2023-12-06
==================
* fix: avoid minimizing single nested subdocs if they are required #14151 #14058
* fix(populate): allow deselecting discriminator key when populating #14155 #3230
* fix: allow adding discriminators using Schema.prototype.discriminator() to subdocuments after defining parent schema #14131 #14109
* fix(schema): avoid creating unnecessary clone of schematype in nested array so nested document arrays use correct constructor #14128 #14101
* fix(populate): call transform object with single id instead of array when populating a justOne path under an array #14135 #14073
* types: add back mistakenly removed findByIdAndRemove() function signature #14136 #14132

7.6.6 / 2023-11-27
==================
* perf: avoid double-running setter logic when calling `push()` #14120 #11380
* fix(populate): set populated docs in correct order when populating virtual underneath doc array with justOne #14105 #14018
* fix: bump mongodb driver -> 5.9.1 #14084 #13829 [lorand-horvath](https://github.com/lorand-horvath)
* types: allow defining document array using [{ prop: String }] syntax #14095 #13424
* types: correct types for when includeResultMetadata: true is set #14078 #13987 [prathamVaidya](https://github.com/prathamVaidya)
* types(query): base filters and projections off of RawDocType instead of DocType so autocomplete doesn't show populate #14118 #14077
* types: make property names show up in intellisense for UpdateQuery #14123 #14090
* types(model): support calling Model.validate() with pathsToSkip option #14088 #14003
* docs: remove "DEPRECATED" warning mistakenly added to read() tags param #13980

7.6.5 / 2023-11-14
==================
* fix: handle update validators and single nested doc with numeric paths #14066 #13977
* fix: handle recursive schema array in discriminator definition #14068 #14055
* fix: diffIndexes treats namespace error as empty #14048 #14029
* docs(migrating_to_7): add note about requiring new with ObjectId #14021 #14020

6.12.3 / 2023-11-07
===================
* fix(ChangeStream): correctly handle hydrate option when using change stream as stream instead of iterator #14052
* fix(schema): fix dangling reference to virtual in tree after `removeVirtual()` #14019 #13085
* fix(document): avoid unmarking modified on nested path if no initial value stored and already modified #14053 #14024
* fix(document): consistently avoid marking subpaths of nested paths as modified #14053 #14022

7.6.4 / 2023-10-30
==================
* fix(connection): retain modified status for documents created outside a transaction during transaction retries #14017 #13973
* fix(schema): handle recursive schemas in discriminator definitions #14011 #13978
* fix: handle casting $or underneath $elemMatch #14007 #13974
* fix(populate): allow using options: { strictPopulate: false } to disable strict populate #13863
* docs: fix differences between sample codes and documentation #13998 [suzuki](https://github.com/suzuki)
* docs: fix missing import and change wrong variable name #13992 [suzuki](https://github.com/suzuki)

6.12.2 / 2023-10-25
===================
* fix: add fullPath to ValidatorProps #13995 [Freezystem](https://github.com/Freezystem)

7.6.3 / 2023-10-17
==================
* fix(populate): handle multiple spaces when specifying paths to populate using space-delimited paths #13984 #13951
* fix(update): avoid applying defaults on query filter when upserting with empty update #13983 #13962
* fix(model): add versionKey to bulkWrite when inserting or upserting #13981 #13944
* docs: fix typo in timestamps docs #13976 [danielcoker](https://github.com/danielcoker)

7.6.2 / 2023-10-13
==================
* perf: avoid storing a separate entry in schema subpaths for every element in an array #13953 #13874
* fix(document): avoid triggering setter when initializing Model.prototype.collection to allow defining collection as a schema path name #13968 #13956
* fix(model): make bulkSave() save changes in discriminator paths if calling bulkSave() on base model #13959 #13907
* fix(document): allow calling $model() with no args for TypeScript #13963 #13878
* fix(schema): handle embedded discriminators defined using Schema.prototype.discriminator() #13958 #13898
* types(model): make InsertManyResult consistent with return type of insertMany #13965 #13904
* types(models): add cleaner type definitions for insertMany() with no generics to prevent errors when using insertMany() in generic classes #13964 #13957
* types(schematypes): allow defining map path using type: 'Map' in addition to type: Map #13960 #13755

6.12.1 / 2023-10-12
===================
* fix(mongoose): correctly handle global applyPluginsToChildSchemas option #13945 #13887 [hasezoey](https://github.com/hasezoey)
* fix: Document.prototype.isModified support for a string of keys as first parameter #13940 #13674 [k-chop](https://github.com/k-chop)

7.6.1 / 2023-10-09
==================
* fix: bump bson to match mongodb@5.9.0 exactly #13947 [hasezoey](https://github.com/hasezoey)
* fix: raw result deprecation message #13954 [simllll](https://github.com/simllll)
* type: add types for includeResultMetadata #13955 [simllll](https://github.com/simllll)
* perf(npmignore): ignore newer files #13946 [hasezoey](https://github.com/hasezoey)
* perf: move mocha config from package.json to mocharc #13948 [hasezoey](https://github.com/hasezoey)

7.6.0 / 2023-10-06
==================
* feat: upgrade mongodb node driver -> 5.9.0 #13927 #13926 [sanguineti](https://github.com/sanguineti)
* fix: avoid CastError when passing different value of discriminator key in `$or` #13938 #13906

7.5.4 / 2023-10-04
==================
* fix: avoid stripping out `id` property when `_id` is set #13933 #13892 #13867
Expand Down
18 changes: 13 additions & 5 deletions docs/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,24 @@ The `mongoose.createConnection()` function takes the same arguments as
const conn = mongoose.createConnection('mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]', options);
```

This [connection](api/connection.html#connection_Connection) object is then used to
create and retrieve [models](api/model.html#model_Model). Models are
**always** scoped to a single connection.
This [connection](api/connection.html#connection_Connection) object is then used to create and retrieve [models](api/model.html#model_Model).
Models are **always** scoped to a single connection.

```javascript
const UserModel = conn.model('User', userSchema);
```

If you use multiple connections, you should make sure you export schemas,
**not** models. Exporting a model from a file is called the *export model pattern*.
The `createConnection()` function returns a connection instance, not a promise.
If you want to use `await` to make sure Mongoose successfully connects to MongoDB, use the [`asPromise()` function](api/connection.html#Connection.prototype.asPromise()):

```javascript
// `asPromise()` returns a promise that resolves to the connection
// once the connection succeeds, or rejects if connection failed.
const conn = await mongoose.createConnection(connectionString).asPromise();
```

If you use multiple connections, you should make sure you export schemas, **not** models.
Exporting a model from a file is called the *export model pattern*.
The export model pattern is limited because you can only use one connection.

```javascript
Expand Down
2 changes: 1 addition & 1 deletion docs/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const root = 'https://mongoosejs.azurewebsites.net/api';

const defaultVersion = '7.x';
const defaultVersion = '8.x';
const versionFromUrl = window.location.pathname.match(/^\/docs\/(\d+\.x)/);
const version = versionFromUrl ? versionFromUrl[1] : defaultVersion;

Expand Down
18 changes: 18 additions & 0 deletions docs/migrating_to_7.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ If you're still on Mongoose 5.x, please read the [Mongoose 5.x to 6.x migration
* [Removed `remove()`](#removed-remove)
* [Dropped callback support](#dropped-callback-support)
* [Removed `update()`](#removed-update)
* [ObjectId requires `new`](#objectid-requires-new)
* [Discriminator schemas use base schema options by default](#discriminator-schemas-use-base-schema-options-by-default)
* [Removed `castForQueryWrapper()`, updated `castForQuery()` signature](#removed-castforquerywrapper)
* [Copy schema options in `Schema.prototype.add()`](#copy-schema-options-in-schema-prototype-add)
Expand Down Expand Up @@ -178,6 +179,23 @@ await Model.updateOne(filter, update);
await doc.updateOne(update);
```

<h2 id="objectid-requires-new"><a href="#objectid-requires-new">ObjectId requires <code>new</code></a></h2>

In Mongoose 6 and older, you could define a new ObjectId without using the `new` keyword:

```javascript
// Works in Mongoose 6
// Throws "Class constructor ObjectId cannot be invoked without 'new'" in Mongoose 7
const oid = mongoose.Types.ObjectId('0'.repeat(24));
```

In Mongoose 7, `ObjectId` is now a [JavaScript class](https://masteringjs.io/tutorials/fundamentals/class), so you need to use the `new` keyword.

```javascript
// Works in Mongoose 6 and Mongoose 7
const oid = new mongoose.Types.ObjectId('0'.repeat(24));
```

<h2 id="discriminator-schemas-use-base-schema-options-by-default"><a href="#discriminator-schemas-use-base-schema-options-by-default">Discriminator schemas use base schema options by default</a></h2>

When you use `Model.discriminator()`, Mongoose will now use the discriminator base schema's options by default.
Expand Down
2 changes: 1 addition & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const playerSchema = new Schema({ /* ... */ });
playerSchema.plugin(loadedAtPlugin);
```

We just added last-modified behavior to both our `Game` and `Player` schemas and declared an index on the `lastMod` path of our Games to boot. Not bad for a few lines of code.
We just added loaded-time behavior to both our `Game` and `Player` schemas and declared an index on the `loadedAt` path of our Games to boot. Not bad for a few lines of code.

<h2 id="global"><a href="#global">Global Plugins</a></h2>

Expand Down
2 changes: 1 addition & 1 deletion docs/timestamps.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Mongoose: users.findOneAndUpdate({}, { '$setOnInsert': { createdAt: new Date("Su

Notice the `$setOnInsert` for `createdAt` and `$set` for `updatedAt`.
MongoDB's [`$setOnInsert` operator](https://www.mongodb.com/docs/manual/reference/operator/update/setOnInsert/) applies the update only if a new document is [upserted](https://masteringjs.io/tutorials/mongoose/upsert).
So, for example, if you want to *only* set `updatedAt` if the document if a new document is created, you can disable the `updatedAt` timestamp and set it yourself as shown below:
So, for example, if you want to *only* set `updatedAt` if a new document is created, you can disable the `updatedAt` timestamp and set it yourself as shown below:

```javascript
await User.findOneAndUpdate({}, { $setOnInsert: { updatedAt: new Date() } }, {
Expand Down
38 changes: 37 additions & 1 deletion docs/tutorials/findoneandupdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ However, there are some cases where you need to use [`findOneAndUpdate()`](https
* [Atomic Updates](#atomic-updates)
* [Upsert](#upsert)
* [The `rawResult` Option](#raw-result)
* [Updating Discriminator Keys](#updating-discriminator-keys)

## Getting Started

As the name implies, `findOneAndUpdate()` finds the first document that matches a given `filter`, applies an `update`, and returns the document. By default, `findOneAndUpdate()` returns the document as it was **before** `update` was applied.
As the name implies, `findOneAndUpdate()` finds the first document that matches a given `filter`, applies an `update`, and returns the document.
The `findOneAndUpdate()` function has the following signature:

```javascript
function findOneAndUpdate(filter, update, options) {}
```

By default, `findOneAndUpdate()` returns the document as it was **before** `update` was applied.

```acquit
[require:Tutorial.*findOneAndUpdate.*basic case]
Expand Down Expand Up @@ -78,3 +86,31 @@ Here's what the `res` object from the above example looks like:
age: 29 },
ok: 1 }
```

## Updating Discriminator Keys

Mongoose prevents updating the [discriminator key](../discriminators.html#discriminator-keys) using `findOneAndUpdate()` by default.
For example, suppose you have the following discriminator models.

```javascript
const eventSchema = new mongoose.Schema({ time: Date });
const Event = db.model('Event', eventSchema);

const ClickedLinkEvent = Event.discriminator(
'ClickedLink',
new mongoose.Schema({ url: String })
);

const SignedUpEvent = Event.discriminator(
'SignedUp',
new mongoose.Schema({ username: String })
);
```

Mongoose will remove `__t` (the default discriminator key) from the `update` parameter, if `__t` is set.
This is to prevent unintentional updates to the discriminator key; for example, if you're passing untrusted user input to the `update` parameter.
However, you can tell Mongoose to allow updating the discriminator key by setting the `overwriteDiscriminatorKey` option to `true` as shown below.

```acquit
[require:use overwriteDiscriminatorKey to change discriminator key]
```
4 changes: 2 additions & 2 deletions docs/typescript/query-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The 2nd generic parameter, `TQueryHelpers`, should be an interface that contains
Below is an example of creating a `ProjectModel` with a `byName` query helper.

```typescript
import { HydratedDocument, Model, Query, Schema, model } from 'mongoose';
import { HydratedDocument, Model, QueryWithHelpers, Schema, model, connect } from 'mongoose';

interface Project {
name?: string;
Expand Down Expand Up @@ -64,7 +64,7 @@ ProjectSchema.query.byName = function byName(
};

// 2nd param to `model()` is the Model class to return.
const ProjectModel = model<Project, ProjectModelType>('Project', schema);
const ProjectModel = model<Project, ProjectModelType>('Project', ProjectSchema);

run().catch(err => console.log(err));

Expand Down
10 changes: 2 additions & 8 deletions docs/version-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ We ship all new bug fixes and features to 7.x.

## Mongoose 6

Mongoose 6.x (released August 24, 2021) is currently in legacy support.
We will continue to ship bug fixes to Mongoose 6 until August 24, 2023.
After August 24, 2023, we will only ship security fixes, and backport requested fixes to Mongoose 6.
Mongoose 6.x (released August 24, 2021) is currently only receiving security fixes and requested bug fixes as of August 24, 2023.
Please open a [bug report on GitHub](https://github.com/Automattic/mongoose/issues/new?assignees=&labels=&template=bug.yml) to request backporting a fix to Mongoose 6.

We are **not** actively backporting any new features from Mongoose 7 into Mongoose 6.
Until August 24, 2023, we will backport requested features into Mongoose 6; please open a [feature request on GitHub](https://github.com/Automattic/mongoose/issues/new?assignees=&labels=enhancement%2Cnew+feature&template=feature.yml) to request backporting a feature into Mongoose 6.
After August 24, 2023, we will not backport any new features into Mongoose 6.

We do not currently have a formal end of life (EOL) date for Mongoose 6.
However, we will not end support for Mongoose 6 until at least January 1, 2024.
However, we will not end support for Mongoose 6 until at least April 1, 2024.

## Mongoose 5

Expand Down
2 changes: 1 addition & 1 deletion lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ Aggregate.prototype.unionWith = function(options) {
* await Model.aggregate(pipeline).read('primaryPreferred');
*
* @param {String|ReadPreference} pref one of the listed preference options or their aliases
* @param {Array} [tags] optional tags for this query. DEPRECATED
* @param {Array} [tags] optional tags for this query.
* @return {Aggregate} this
* @api public
* @see mongodb https://www.mongodb.com/docs/manual/applications/replication/#read-preference
Expand Down
4 changes: 4 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,10 @@ function _resetSessionDocuments(session) {
doc.$__.activePaths.states.modify = {};
}
for (const path of state.modifiedPaths) {
const currentState = doc.$__.activePaths.paths[path];
if (currentState != null) {
delete doc.$__.activePaths[currentState][path];
}
doc.$__.activePaths.paths[path] = 'modify';
doc.$__.activePaths.states.modify[path] = true;
}
Expand Down
Loading

0 comments on commit a4c9e10

Please sign in to comment.