Skip to content

Commit

Permalink
docs(migrating_to_6): add a note about minimize and toObject() be…
Browse files Browse the repository at this point in the history
…havior change in v5.10.5

Fix #10827
  • Loading branch information
vkarpov15 committed Nov 29, 2021
1 parent 903b8b4 commit 30574ae
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/migrating_to_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ If you're still on Mongoose 4.x, please read the [Mongoose 4.x to 5.x migration
* [Removed Validator `isAsync`](#removed-validator-isasync)
* [Removed `safe`](#removed-safe)
* [SchemaType `set` parameters now use `priorValue` as the second parameter instead of `self`](#schematype-set-parameters)
* [`toObject()` and `toJSON()` Use Nested Schema `minimize`](#toobject-and-tojson-use-nested-schema-minimize)
* [TypeScript changes](#typescript-changes)

<h3 id="version-requirements"><a href="#version-requirements">Version Requirements</a></h3>
Expand Down Expand Up @@ -359,6 +360,37 @@ const user = new User({ name: 'Robert Martin' });
console.log(user.name); // 'robert martin'
```

<h3 id="toobject-and-tojson-use-nested-schema-minimize"><a href="#toobject-and-tojson-use-nested-schema-minimize">`toObject()` and `toJSON()` Use Nested Schema `minimize`</a></h3>

This change was technically released with 5.10.5, but [caused issues for users migrating from 5.9.x to 6.x](https://github.com/Automattic/mongoose/issues/10827).
In Mongoose `< 5.10.5`, `toObject()` and `toJSON()` would use the top-level schema's `minimize` option by default.

```javascript
const child = new Schema({ thing: Schema.Types.Mixed });
const parent = new Schema({ child }, { minimize: false });
const Parent = model('Parent', parent);
const p = new Parent({ child: { thing: {} } });

// In v5.10.4, would contain `child.thing` because `toObject()` uses `parent` schema's `minimize` option
// In `>= 5.10.5`, `child.thing` is omitted because `child` schema has `minimize: true`
console.log(p.toObject());
```

As a workaround, you can either explicitly pass `minimize` to `toObject()` or `toJSON()`:

```javascript
console.log(p.toObject({ minimize: false }));
```

Or define the `child` schema inline (Mongoose 6 only) to inherit the parent's `minimize` option.

```javascript
const parent = new Schema({
// Implicitly creates a new schema with the top-level schema's `minimize` option.
child: { type: { thing: Schema.Types.Mixed } }
}, { minimize: false });
```

## 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

0 comments on commit 30574ae

Please sign in to comment.