diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dba26f322..74fd4623ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/migrating_to_6.md b/docs/migrating_to_6.md index 36d670fc34..bfa98fa3ec 100644 --- a/docs/migrating_to_6.md +++ b/docs/migrating_to_6.md @@ -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)

Version Requirements

@@ -144,9 +146,16 @@ if (existingUser) {

strictQuery is now equal to strict by default

~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 }); @@ -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). +

Lodash .isEmpty() returns true for ObjectIds

+ +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 +} +``` + +

Removed reconnectTries and reconnectInterval options

+ +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. + +

Removed mongoose.modelSchemas

+ +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(schemaDefinition)` with `new Schema(schemaDefinition)` @@ -542,9 +585,3 @@ schema.virtual('myVirtual').get(function() { }); ``` -

Removed reconnectTries and reconnectInterval options

- -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. diff --git a/lib/query.js b/lib/query.js index 3f6fc76fb8..4587ea37d0 100644 --- a/lib/query.js +++ b/lib/query.js @@ -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);