Skip to content

Commit

Permalink
Merge branch '6.x' into 7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 18, 2024
2 parents 572e97a + 22210b1 commit bc2809d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
6.13.4 / 2024-11-15
===================
* fix: save execution stack in query as string #15043 #15039
* docs: clarify strictQuery default will flip-flop in "Migrating to 6.x" #14998 [markstos](https://github.com/markstos)

7.8.2 / 2024-09-25
==================
* fix(projection): avoid setting projection to unknown exclusive/inclusive if elemMatch on a Date, ObjectId, etc. #14894 #14893

6.13.3 / 2024-09-23
===================
* docs(migrating_to_6): document that Lodash _.isEmpty() with ObjectId() as a parameter returns true in Mongoose 6 #11152

6.13.2 / 2024-09-12
===================
* fix(document): make set() respect merge option on deeply nested objects #14870 #14878
Expand Down
57 changes: 47 additions & 10 deletions docs/migrating_to_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ If you're still on Mongoose 4.x, please read the [Mongoose 4.x to 5.x migration
* [SchemaType `set` parameters now use `priorValue` as the second parameter instead of `self`](#schematype-set-parameters)
* [No default model for `Query.prototype.populate()`](#no-default-model-for-query-prototype-populate)
* [`toObject()` and `toJSON()` Use Nested Schema `minimize`](#toobject-and-tojson-use-nested-schema-minimize)
* [TypeScript changes](#typescript-changes)
* [Removed `reconnectTries` and `reconnectInterval` options](#removed-reconnecttries-and-reconnectinterval-options)
* [MongoDB Driver's New URL Parser Incompatible with Some npm Packages](#mongodb-drivers-new-url-parser-incompatible-with-some-npm-packages)
* [Lodash `.isEmpty()` returns false for ObjectIds](#lodash-object-id)
* [mongoose.modelSchemas removed](#model-schemas)
* [TypeScript changes](#typescript-changes)

<h2 id="version-requirements"><a href="#version-requirements">Version Requirements</a></h2>

Expand Down Expand Up @@ -144,9 +146,16 @@ if (existingUser) {
<h2 id="strictquery-is-removed-and-replaced-by-strict"><a href="#strictquery-is-removed-and-replaced-by-strict"><code>strictQuery</code> is now equal to <code>strict</code> by default</a></h2>

~Mongoose no longer supports a `strictQuery` option. You must now use `strict`.~
As of Mongoose 6.0.10, we brought back the `strictQuery` option.
However, `strictQuery` is tied to `strict` by default.
This means that, by default, Mongoose will filter out query filter properties that are not in the schema.
As of Mongoose 6.0.10, we brought back the `strictQuery` option. In Mongoose 6, `strictQuery` is set to `strict` by default. This means that, by default, Mongoose will filter out query filter properties that are not in the schema.

However, this behavior was a source of confusion in some cases, so in Mongoose 7, this default changes back to `false`. So if you want to retain the default behavior of Mongoose 5 as well as Mongoose 7 and later, you can also disable `strictQuery` globally to override:

```javascript
mongoose.set('strictQuery', false);
```
In a test suite, it may be useful to set `strictQuery` to `throw`, which will throw exceptions any time a query references schema that doesn't exist, which could help identify a bug in your tests or code.

Here's an example of the effect of `strictQuery`:

```javascript
const userSchema = new Schema({ name: String });
Expand Down Expand Up @@ -504,6 +513,40 @@ The MongoDB Node driver version that Mongoose 6 uses relies on a [URL parser mod
This can lead to errors like `Invalid URL: mongodb+srv://username:password@development.xyz.mongodb.net/abc` if you use one of the incompatible packages.
[You can find a list of incompatible packages here](https://mongoosejs.com/docs/incompatible_packages).

<h2 id="lodash-object-id"><a href="#lodash-object-id">Lodash <code>.isEmpty()</code> returns true for ObjectIds</a></h2>

Lodash's `isEmpty()` function returns true for primitives and primitive wrappers.
`ObjectId()` is an object wrapper that is treated as a primitive by Mongoose.
But starting in Mongoose 6, `_.isEmpty()` will return true for ObjectIds because of Lodash implementation details.

An ObjectId in mongoose is never empty, so if you're using `isEmpty()` you should check for `instanceof ObjectId`.

```javascript
if (!(val instanceof Types.ObjectId) && _.isEmpty(val)) {
// Handle empty object here
}
```

<h2 id="removed-reconnecttries-and-reconnectinterval-options"><a href="#removed-reconnecttries-and-reconnectinterval-options">Removed <code>reconnectTries</code> and <code>reconnectInterval</code> options</a></h2>

The `reconnectTries` and `reconnectInterval` options have been removed since they are no longer necessary.

The MongoDB node driver will always attempt to retry any operation for up to `serverSelectionTimeoutMS`, even if MongoDB is down for a long period of time.
So, it will never run out of retries or try to reconnect to MongoDB.

<h2 id="model-schemas"><a href="#model-schemas">Removed <code>mongoose.modelSchemas</code></a></h2>

The `mongoose.modelSchemas` property was removed. This may have been used to delete a model schema.

```javascript
// before
delete mongoose.modelSchemas.User;

// with Mongoose 6.x
delete mongoose.deleteModel('User');
```


## TypeScript changes

The `Schema` class now takes 3 generic params instead of 4. The 3rd generic param, `SchemaDefinitionType`, is now the same as the 1st generic param `DocType`. Replace `new Schema<UserDocument, UserModel, User>(schemaDefinition)` with `new Schema<UserDocument, UserModel>(schemaDefinition)`
Expand Down Expand Up @@ -542,9 +585,3 @@ schema.virtual('myVirtual').get(function() {
});
```

<h2 id="removed-reconnecttries-and-reconnectinterval-options"><a href="#removed-reconnecttries-and-reconnectinterval-options">Removed <code>reconnectTries</code> and <code>reconnectInterval</code> options</a></h2>

The `reconnectTries` and `reconnectInterval` options have been removed since they are no longer necessary.

The MongoDB node driver will always attempt to retry any operation for up to `serverSelectionTimeoutMS`, even if MongoDB is down for a long period of time.
So, it will never run out of retries or try to reconnect to MongoDB.
4 changes: 2 additions & 2 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4431,10 +4431,10 @@ Query.prototype.exec = async function exec(op) {
str = str.slice(0, 60) + '...';
}
const err = new MongooseError('Query was already executed: ' + str);
err.originalStack = this._executionStack.stack;
err.originalStack = this._executionStack;
throw err;
} else {
this._executionStack = new Error();
this._executionStack = new Error().stack;
}

await _executePreExecHooks(this);
Expand Down

0 comments on commit bc2809d

Please sign in to comment.