Skip to content

Commit bbbfa95

Browse files
authored
Merge pull request #14021 from Automattic/vkarpov15/gh-14020
docs(migrating_to_7): add note about requiring `new` with `ObjectId`
2 parents f73ee66 + ad5b21e commit bbbfa95

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/migrating_to_7.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ If you're still on Mongoose 5.x, please read the [Mongoose 5.x to 6.x migration
1515
* [Removed `remove()`](#removed-remove)
1616
* [Dropped callback support](#dropped-callback-support)
1717
* [Removed `update()`](#removed-update)
18+
* [ObjectId requires `new`](#objectid-requires-new)
1819
* [Discriminator schemas use base schema options by default](#discriminator-schemas-use-base-schema-options-by-default)
1920
* [Removed `castForQueryWrapper()`, updated `castForQuery()` signature](#removed-castforquerywrapper)
2021
* [Copy schema options in `Schema.prototype.add()`](#copy-schema-options-in-schema-prototype-add)
@@ -178,6 +179,23 @@ await Model.updateOne(filter, update);
178179
await doc.updateOne(update);
179180
```
180181

182+
<h2 id="objectid-requires-new"><a href="#objectid-requires-new">ObjectId requires <code>new</code></a></h2>
183+
184+
In Mongoose 6 and older, you could define a new ObjectId without using the `new` keyword:
185+
186+
```javascript
187+
// Works in Mongoose 6
188+
// Throws "Class constructor ObjectId cannot be invoked without 'new'" in Mongoose 7
189+
const oid = mongoose.Types.ObjectId('0'.repeat(24));
190+
```
191+
192+
In Mongoose 7, `ObjectId` is now a [JavaScript class](https://masteringjs.io/tutorials/fundamentals/class), so you need to use the `new` keyword.
193+
194+
```javascript
195+
// Works in Mongoose 6 and Mongoose 7
196+
const oid = new mongoose.Types.ObjectId('0'.repeat(24));
197+
```
198+
181199
<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>
182200

183201
When you use `Model.discriminator()`, Mongoose will now use the discriminator base schema's options by default.

0 commit comments

Comments
 (0)