Skip to content

Commit bb7c021

Browse files
committed
docs(defaults): clarify that setDefaultsOnInsert is true by default in 6.x
Fix #10643
1 parent 36d23ce commit bb7c021

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

docs/defaults.md

+7-10
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,17 @@ execute that function and use the return value as the default.
2323

2424
### The `setDefaultsOnInsert` Option
2525

26-
By default, mongoose only applies defaults when you create a new document.
27-
It will **not** set defaults if you use `update()` and
28-
`findOneAndUpdate()`. However, mongoose 4.x lets you opt-in to this
29-
behavior using the `setDefaultsOnInsert` option.
26+
Mongoose also sets defaults on `update()` and `findOneAndUpdate()` when the `upsert` option is set by adding your schema's defaults to a [MongoDB `$setOnInsert` operator](https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/).
27+
You can disable this behavior by setting the `setDefaultsOnInsert` option to `false`.
3028

31-
#### Important
29+
```javascript
30+
[require:The `setDefaultsOnInsert` option]
31+
```
3232

33-
The `setDefaultsOnInsert` option relies on the
34-
[MongoDB `$setOnInsert` operator](https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/).
35-
The `$setOnInsert` operator was introduced in MongoDB 2.4. If you're
36-
using MongoDB server < 2.4.0, do **not** use `setDefaultsOnInsert`.
33+
You can also set `setDefaultsOnInsert` to `false` globally:
3734

3835
```javascript
39-
[require:The `setDefaultsOnInsert` option]
36+
mongoose.set('setDefaultsOnInsert', false);`
4037
```
4138

4239
### Default functions and `this`

test/docs/defaults.test.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('defaults docs', function() {
8989
* inserted, not if the upsert updated an existing document.
9090
*
9191
*/
92-
it('The `setDefaultsOnInsert` option', function() {
92+
it('The `setDefaultsOnInsert` option', async function() {
9393
const schema = new Schema({
9494
title: String,
9595
genre: { type: String, default: 'Action' }
@@ -106,14 +106,21 @@ describe('defaults docs', function() {
106106
upsert: true
107107
};
108108

109-
return Movie.findOneAndUpdate(query, update, options).then(doc => {
110-
doc.genre; // 'Action', Mongoose set a default value.
111-
// acquit:ignore:start
112-
assert.equal(doc.title, 'The Terminator');
113-
assert.equal(doc.genre, 'Action');
114-
// acquit:ignore:end
115-
return doc;
116-
});
109+
let doc = await Movie.findOneAndUpdate(query, update, options).lean();
110+
doc.genre; // 'Action', Mongoose set a default value.
111+
// acquit:ignore:start
112+
assert.equal(doc.title, 'The Terminator');
113+
assert.equal(doc.genre, 'Action');
114+
// acquit:ignore:end
115+
116+
await Movie.deleteMany({});
117+
118+
doc = await Movie.findOneAndUpdate(query, update, { ...options, setDefaultsOnInsert: false }).lean();
119+
doc.genre; // undefined, Mongoose did not set a default value
120+
// acquit:ignore:start
121+
assert.equal(doc.title, 'The Terminator');
122+
assert.equal(doc.genre, void 0);
123+
// acquit:ignore:end
117124
});
118125

119126
/**

0 commit comments

Comments
 (0)