Skip to content

Commit

Permalink
Merge branch 'master' into 8.6
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Aug 28, 2024
2 parents 13872ee + fb0febb commit 0a93334
Show file tree
Hide file tree
Showing 50 changed files with 685 additions and 412 deletions.
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
8.5.5 / 2024-08-28
==================
* fix(populate): fix a couple of other places where Mongoose gets the document's _id with getters #14833 #14827 #14759
* fix(discriminator): shallow clone Schema.prototype.obj before merging schemas to avoid modifying original obj #14821
* types: fix schema type based on timestamps schema options value #14829 #14825 [ark23CIS](https://github.com/ark23CIS)

8.5.4 / 2024-08-23
==================
* fix: add empty string check for collection name passed #14806 [Shubham2552](https://github.com/Shubham2552)
* docs(model): add 'throw' as valid strict value for bulkWrite() and add some more clarification on throwOnValidationError #14809

7.8.1 / 2024-08-19
==================
* fix(query): handle casting $switch in $expr #14761
* docs(mongoose): remove out-of-date callback-based example for mongoose.connect() #14811 #14810

8.5.3 / 2024-08-13
==================
* fix(document): call required functions on subdocuments underneath nested paths with correct context #14801 #14788
Expand All @@ -20,6 +36,11 @@
* types: allow calling SchemaType.cast() without parent and init parameters #14756 #14748 #9076
* docs: fix a wrong example in v6 migration guide #14758 [abdelrahman-elkady](https://github.com/abdelrahman-elkady)

7.8.0 / 2024-07-23
==================
* feat: add transactionAsyncLocalStorage option to opt in to automatically setting session on all transactions #14744 #14742 #14583 #13889
* types(query): fix usage of "RawDocType" where "DocType" should be passed #14737 [hasezoey](https://github.com/hasezoey)

8.5.1 / 2024-07-12
==================
* perf(model): performance improvements for insertMany() #14724
Expand Down Expand Up @@ -59,6 +80,10 @@
* types: add $documents pipeline stage and fix $unionWith type #14666 [nick-statsig](https://github.com/nick-statsig)
* docs(findoneandupdate): improve example that shows findOneAndUpdate() returning doc before updates were applied #14671 #14670

7.7.0 / 2024-06-18
==================
* feat(model): add throwOnValidationError option for opting into getting MongooseBulkWriteError if all valid operations succeed in bulkWrite() and insertMany() #14599 #14587 #14572 #13410

8.4.3 / 2024-06-17
==================
* fix: remove 0x flamegraph files from release
Expand All @@ -72,13 +97,28 @@
* types: avoid inferring Boolean, Buffer, ObjectId as Date in schema definitions under certain circumstances #14667 #14630
* docs: add note about parallelism in transations #14647 [fiws](https://github.com/fiws)

6.13.0 / 2024-06-06
===================
* feat(model): add throwOnValidationError option for opting into getting MongooseBulkWriteError if all valid operations succeed in bulkWrite() and insertMany() #14599 #14587 #14572 #13410

7.6.13 / 2024-06-05
===================
* fix(query): shallow clone $or and $and array elements to avoid mutating query filter arguments #14614 #14610
* types: pass DocType down to subdocuments so HydratedSingleSubdocument and HydratedArraySubdocument toObject() returns correct type #14612 #14601
* docs(migrating_to_7): add id setter to Mongoose 7 migration guide #14645 #13672

8.4.1 / 2024-05-31
==================
* fix: pass options to clone instead of get in applyVirtuals #14606 #14543 [andrews05](https://github.com/andrews05)
* fix(document): fire pre validate hooks on 5 level deep single nested subdoc when modifying after save() #14604 #14591
* fix: ensure buildBulkWriteOperations target shard if shardKey is set #14622 #14621 [matlpriceshape](https://github.com/matlpriceshape)
* types: pass DocType down to subdocuments so HydratedSingleSubdocument and HydratedArraySubdocument toObject() returns correct type #14612 #14601

6.12.9 / 2024-05-24
===================
* fix(cast): cast $comment to string in query filters #14590 #14576
* types(model): allow passing strict type checking override to create() #14571 #14548

7.6.12 / 2024-05-21
===================
* fix(array): avoid converting to $set when calling pull() on an element in the middle of the array #14531 #14502
Expand Down Expand Up @@ -559,6 +599,7 @@
==================
* perf: speed up mapOfSubdocs benchmark by 4x by avoiding unnecessary O(n^2) loop in getPathsToValidate() #13614
* feat: upgrade to MongoDB Node.js driver 5.7.0 #13591
* feat: add `id` setter which allows modifying `_id` by setting `id` (Note this change was reverted in Mongoose 8) #13517
* feat: support generating custom cast error message with a function #13608 #3162
* feat(query): support MongoDB driver's includeResultMetadata option for findOneAndUpdate #13584 #13539
* feat(connection): add Connection.prototype.removeDb() for removing a related connection #13580 #11821
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/typescript/simple/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"mongoose": "file:../../../mongoose.tgz",
"typescript": "4.9.x"
"typescript": "5.5.x"
},
"scripts": {
"benchmark": "tsc --extendedDiagnostics"
Expand Down
2 changes: 1 addition & 1 deletion docs/async-await.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function doStuffWithUser() {
}
```

<h2 id="queries">Async/Await with Mongoose Queries</h2>
## Async/Await with Mongoose Queries {#queries}

Under the hood, [async/await is syntactic sugar](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) over the Promise API.
Due to the surprisingly simple way promises are implemented in JavaScript, the keyword `await` will try to unwrap any object with a property whose key is the string ‘then’ and whose value is a function.
Expand Down
2 changes: 1 addition & 1 deletion docs/browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const mongoose = require('mongoose/browser');
import mongoose from 'mongoose/browser';
```

<h2 id="usage">Using the Browser Library</h2>
## Using the Browser Library {#usage}

Mongoose's browser library is very limited. The only use case it supports is validating documents as shown below.

Expand Down
32 changes: 16 additions & 16 deletions docs/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ See the [mongodb connection string spec](http://www.mongodb.com/docs/manual/refe
<li><a href="#multi-tenant-connections">Multi Tenant Connections</a></li>
</ul>

<h2 id="buffering"><a href="#buffering">Operation Buffering</a></h2>
## Operation Buffering {#buffering}

Mongoose lets you start using your models immediately, without waiting for
mongoose to establish a connection to MongoDB.
Expand Down Expand Up @@ -95,7 +95,7 @@ const Model = mongoose.model('Test', schema);
await Model.createCollection();
```

<h2 id="error-handling"><a href="#error-handling">Error Handling</a></h2>
## Error Handling {#error-handling}

There are two classes of errors that can occur with a Mongoose connection.

Expand Down Expand Up @@ -129,7 +129,7 @@ mongoose.connection.on('error', err => {
Note that Mongoose does not necessarily emit an 'error' event if it loses connectivity to MongoDB. You should
listen to the `disconnected` event to report when Mongoose is disconnected from MongoDB.

<h2 id="options"><a href="#options">Options</a></h2>
## Options {#options}

The `connect` method also accepts an `options` object which will be passed
on to the underlying MongoDB driver.
Expand Down Expand Up @@ -158,7 +158,7 @@ Below are some of the options that are important for tuning Mongoose.
* `serverSelectionTimeoutMS` - The MongoDB driver will try to find a server to send any given operation to, and keep retrying for `serverSelectionTimeoutMS` milliseconds. If not set, the MongoDB driver defaults to using `30000` (30 seconds).
* `heartbeatFrequencyMS` - The MongoDB driver sends a heartbeat every `heartbeatFrequencyMS` to check on the status of the connection. A heartbeat is subject to `serverSelectionTimeoutMS`, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a `'disconnected'` event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits `'disconnected'`. We recommend you do **not** set this setting below 1000, too many heartbeats can lead to performance degradation.

<h2 id="serverselectiontimeoutms"><a href="#serverselectiontimeoutms">serverSelectionTimeoutMS</a></h2>
## serverSelectionTimeoutMS {#serverselectiontimeoutms}

The `serverSelectionTimeoutMS` option is extremely important: it controls how long the MongoDB Node.js driver will attempt to retry any operation before erroring out.
This includes initial connection, like `await mongoose.connect()`, as well as any operations that make requests to MongoDB, like `save()` or `find()`.
Expand Down Expand Up @@ -208,7 +208,7 @@ for (let i = 0; i < 3; ++i) {
}
```

<h2 id="callback"><a href="#callback">Callback</a></h2>
## Callback {#callback}

The `connect()` function also accepts a callback parameter and returns a
[promise](promises.html).
Expand All @@ -225,7 +225,7 @@ mongoose.connect(uri, options).then(
);
```

<h2 id="connection-string-options"><a href="#connection-string-options">Connection String Options</a></h2>
## Connection String Options {#connection-string-options}

You can also specify driver options in your connection string as
[parameters in the query string](https://en.wikipedia.org/wiki/Query_string)
Expand Down Expand Up @@ -258,7 +258,7 @@ are closely associated with the hostname and authentication information.
* `authSource` - The database to use when authenticating with `user` and `pass`. In MongoDB, [users are scoped to a database](https://www.mongodb.com/docs/manual/tutorial/manage-users-and-roles/). If you are getting an unexpected login failure, you may need to set this option.
* `family` - Whether to connect using IPv4 or IPv6. This option passed to [Node.js' `dns.lookup()`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) function. If you don't specify this option, the MongoDB driver will try IPv6 first and then IPv4 if IPv6 fails. If your `mongoose.connect(uri)` call takes a long time, try `mongoose.connect(uri, { family: 4 })`

<h2 id="connection-events"><a href="#connection-events">Connection Events</a></h2>
## Connection Events {#connection-events}

Connections inherit from [Node.js' `EventEmitter` class](https://nodejs.org/api/events.html#events_class_eventemitter),
and emit events when something happens to the connection, like losing
Expand Down Expand Up @@ -304,13 +304,13 @@ conn.on('disconnecting', () => console.log('disconnecting'));
conn.on('close', () => console.log('close'));
```

<h2 id="keepAlive"><a href="#keepAlive">A note about keepAlive</a></h2>
## A note about keepAlive {#keepAlive}

Before Mongoose 5.2.0, you needed to enable the `keepAlive` option to initiate [TCP keepalive](https://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html) to prevent `"connection closed"` errors.
However, `keepAlive` has been `true` by default since Mongoose 5.2.0, and the `keepAlive` is deprecated as of Mongoose 7.2.0.
Please remove `keepAlive` and `keepAliveInitialDelay` options from your Mongoose connections.

<h2 id="replicaset_connections"><a href="#replicaset_connections">Replica Set Connections</a></h2>
## Replica Set Connections {#replicaset_connections}

To connect to a replica set you pass a comma delimited list of hosts to
connect to rather than a single host.
Expand All @@ -331,7 +331,7 @@ To connect to a single node replica set, specify the `replicaSet` option.
mongoose.connect('mongodb://host1:port1/?replicaSet=rsName');
```

<h2 id="server-selection"><a href="#server-selection">Server Selection</a></h2>
## Server Selection {#server-selection}

The underlying MongoDB driver uses a process known as [server selection](https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst) to connect to MongoDB and send operations to MongoDB.
If the MongoDB driver can't find a server to send an operation to after `serverSelectionTimeoutMS`,
Expand Down Expand Up @@ -366,7 +366,7 @@ mongoose.connect(uri, {
}).catch(err => console.log(err.reason));
```

<h2 id="replicaset-hostnames"><a href="#replicaset-hostnames">Replica Set Host Names</a></h2>
## Replica Set Host Names {#replicaset-hostnames}

MongoDB replica sets rely on being able to reliably figure out the domain name for each member.
On Linux and OSX, the MongoDB server uses the output of the [`hostname` command](https://linux.die.net/man/1/hostname) to figure out the domain name to report to the replica set.
Expand Down Expand Up @@ -399,7 +399,7 @@ if (err.name === 'MongooseServerSelectionError') {
}
```

<h2 id="mongos_connections"><a href="#mongos_connections">Multi-mongos support</a></h2>
## Multi-mongos support {#mongos_connections}

You can also connect to multiple [mongos](https://www.mongodb.com/docs/manual/reference/program/mongos/) instances
for high availability in a sharded cluster. You do
Expand All @@ -410,7 +410,7 @@ for high availability in a sharded cluster. You do
mongoose.connect('mongodb://mongosA:27501,mongosB:27501', cb);
```

<h2 id="multiple_connections"><a href="#multiple_connections">Multiple connections</a></h2>
## Multiple connections {#multiple_connections}

So far we've seen how to connect to MongoDB using Mongoose's default
connection. Mongoose creates a *default connection* when you call `mongoose.connect()`.
Expand Down Expand Up @@ -493,7 +493,7 @@ module.exports = conn;
You can create separate files for each connection, like `connections/web.js` and `connections/mobile.js` if you want to create separate connections for your web API backend and your mobile API backend.
Your business logic can then `require()` or `import` the connection it needs.

<h2 id="connection_pools"><a href="#connection_pools">Connection Pools</a></h2>
## Connection Pools {#connection_pools}

Each `connection`, whether created with `mongoose.connect` or
`mongoose.createConnection` are all backed by an internal configurable
Expand All @@ -512,7 +512,7 @@ mongoose.createConnection(uri);
The connection pool size is important because [MongoDB currently can only process one operation per socket](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
So `maxPoolSize` functions as a cap on the number of concurrent operations.

<h2 id="multi-tenant-connections"><a href="#multi-tenant-connections">Multi Tenant Connections</a></h2>
## Multi Tenant Connections {#multi-tenant-connections}

In the context of Mongoose, a multi-tenant architecture typically means a case where multiple different clients talk to MongoDB through a single Mongoose application.
This typically means each client makes queries and executes updates through a single Mongoose application, but has a distinct MongoDB database within the same MongoDB cluster.
Expand Down Expand Up @@ -592,6 +592,6 @@ app.get('/users/:tenantId', function(req, res) {
app.listen(3000);
```

<h2 id="next">Next Up</h2>
## Next Up {#next}

Now that we've covered connections, let's take a look at [models](models.html).
4 changes: 2 additions & 2 deletions docs/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ that Mongoose users should be aware of. Mongoose provides options to work
around these deprecation warnings, but you need to test whether these options
cause any problems for your application. Please [report any issues on GitHub](https://github.com/Automattic/mongoose/issues/new).

<h2 id="summary"><a href="#summary">Summary</a></h2>
## Summary {#summary}

To fix all deprecation warnings, follow the below steps:

* Replace `rawResult: true` with `includeResultMetadata: true` in `findOneAndUpdate()`, `findOneAndReplace()`, `findOneAndDelete()` calls.

Read below for more a more detailed description of each deprecation warning.

<h2 id="rawresult"><a href="#rawresult"><code>rawResult</code></a></h2>
## `rawResult` {#rawresult}

As of Mongoose 7.4.0, the `rawResult` option to `findOneAndUpdate()` is deprecated.
You should instead use the `includeResultMetadata` option, which the MongoDB Node.js driver's new option that replaces `rawResult`.
Expand Down
12 changes: 6 additions & 6 deletions docs/documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ to documents as stored in MongoDB. Each document is an instance of its
<li><a href="#overwriting">Overwriting</a></li>
</ul>

<h2 id="documents-vs-models"><a href="#documents-vs-models">Documents vs Models</a></h2>
## Documents vs Models {#documents-vs-models}

[Document](api/document.html#Document) and [Model](api/model.html#Model) are distinct
classes in Mongoose. The Model class is a subclass of the Document class.
Expand All @@ -33,7 +33,7 @@ In Mongoose, a "document" generally means an instance of a model.
You should not have to create an instance of the Document class without
going through a model.

<h2 id="retrieving"><a href="#retrieving">Retrieving</a></h2>
## Retrieving {#retrieving}

When you load documents from MongoDB using model functions like [`findOne()`](api/model.html#model_Model-findOne),
you get a Mongoose document back.
Expand All @@ -46,7 +46,7 @@ doc instanceof mongoose.Model; // true
doc instanceof mongoose.Document; // true
```

<h2 id="updating-using-save"><a href="#updating-using-save">Updating Using <code>save()</code></a></h2>
## Updating Using `save()` {#updating-using-save}

Mongoose documents track changes. You can modify a document using vanilla
JavaScript assignments and Mongoose will convert it into [MongoDB update operators](https://www.mongodb.com/docs/manual/reference/operator/update/).
Expand Down Expand Up @@ -81,7 +81,7 @@ doc.name = 'foo';
await doc.save(); // Throws DocumentNotFoundError
```

<h2 id="updating-using-queries"><a href="#updating-using-queries">Updating Using Queries</a></h2>
## Updating Using Queries {#updating-using-queries}

The [`save()`](api/model.html#model_Model-save) function is generally the right
way to update a document with Mongoose. With `save()`, you get full
Expand All @@ -100,7 +100,7 @@ await MyModel.updateMany({}, { $set: { name: 'foo' } });
execute `save()` middleware. If you need save middleware and full validation,
first query for the document and then `save()` it.*

<h2 id="validating"><a href="#validating">Validating</a></h2>
## Validating {#validating}

Documents are casted and validated before they are saved. Mongoose first casts
values to the specified type and then validates them. Internally, Mongoose
Expand Down Expand Up @@ -136,7 +136,7 @@ await Person.updateOne({}, { age: -1 }, { runValidators: true });

Read the [validation](validation.html) guide for more details.

<h2 id="overwriting"><a href="#overwriting">Overwriting</a></h2>
## Overwriting {#overwriting}

There are 2 different ways to overwrite a document (replacing all keys in the
document). One way is to use the
Expand Down
8 changes: 4 additions & 4 deletions docs/geojson.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ polygons. [MongoDB has excellent support for geospatial queries](http://thecodeb
on GeoJSON objects. Let's take a look at how you can use Mongoose to store
and query GeoJSON objects.

<h2 id="points">Point Schema</h2>
## Point Schema {#points}

The most simple structure in GeoJSON is a point. Below is an example point
representing the approximate location of [San Francisco](https://www.google.com/maps/@37.7,-122.5,9z).
Expand Down Expand Up @@ -64,7 +64,7 @@ const citySchema = new mongoose.Schema({
});
```

<h2 id="polygons">Polygon Schema</h2>
## Polygon Schema {#polygons}

GeoJSON polygons let you define an arbitrary shape on a map. For example,
the below polygon is a GeoJSON rectangle that approximates the border
Expand Down Expand Up @@ -106,7 +106,7 @@ const citySchema = new mongoose.Schema({
});
```

<h2 id="querying">Geospatial Queries with Mongoose</h2>
## Geospatial Queries with Mongoose {#querying}

Mongoose queries support the same [geospatial query operators](http://thecodebarbarian.com/80-20-guide-to-mongodb-geospatial-queries)
that the MongoDB driver does. For example, the below script saves a
Expand All @@ -128,7 +128,7 @@ that's a shorthand for `$geoWithin`.
[require:geojson.*within helper]
```

<h2 id="geospatial-indexes">Geospatial Indexes</h2>
## Geospatial Indexes {#geospatial-indexes}

MongoDB supports [2dsphere indexes](https://www.mongodb.com/docs/manual/core/2dsphere/)
for speeding up geospatial queries. Here's how you can define
Expand Down
Loading

0 comments on commit 0a93334

Please sign in to comment.