mongoose 6.2.7 → 6.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +35 -26
- package/CHANGELOG.md +27 -0
- package/dist/browser.umd.js +71 -71
- package/lib/aggregate.js +36 -36
- package/lib/browser.js +4 -4
- package/lib/browserDocument.js +1 -0
- package/lib/connection.js +21 -21
- package/lib/cursor/AggregationCursor.js +2 -3
- package/lib/cursor/QueryCursor.js +3 -3
- package/lib/document.js +75 -47
- package/lib/error/index.js +2 -2
- package/lib/helpers/document/handleSpreadDoc.js +19 -1
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/query/castFilterPath.js +0 -1
- package/lib/index.js +24 -26
- package/lib/model.js +141 -136
- package/lib/options/SchemaArrayOptions.js +2 -2
- package/lib/options/SchemaBufferOptions.js +1 -1
- package/lib/options/SchemaDateOptions.js +9 -2
- package/lib/options/SchemaDocumentArrayOptions.js +3 -3
- package/lib/options/SchemaMapOptions.js +2 -2
- package/lib/options/SchemaNumberOptions.js +3 -3
- package/lib/options/SchemaObjectIdOptions.js +2 -2
- package/lib/options/SchemaStringOptions.js +1 -1
- package/lib/options/SchemaSubdocumentOptions.js +2 -2
- package/lib/options/SchemaTypeOptions.js +3 -3
- package/lib/query.js +253 -225
- package/lib/schema/SubdocumentPath.js +6 -3
- package/lib/schema/array.js +3 -2
- package/lib/schema/boolean.js +4 -4
- package/lib/schema/buffer.js +3 -3
- package/lib/schema/date.js +7 -7
- package/lib/schema/decimal128.js +2 -2
- package/lib/schema/documentarray.js +17 -10
- package/lib/schema/mixed.js +2 -2
- package/lib/schema/number.js +6 -6
- package/lib/schema/objectid.js +4 -4
- package/lib/schema/string.js +14 -14
- package/lib/schema.js +28 -28
- package/lib/schematype.js +78 -68
- package/lib/types/ArraySubdocument.js +1 -1
- package/lib/types/DocumentArray/methods/index.js +2 -2
- package/lib/types/array/index.js +1 -1
- package/lib/types/array/methods/index.js +14 -12
- package/lib/types/buffer.js +1 -1
- package/lib/types/decimal128.js +1 -1
- package/lib/types/objectid.js +1 -1
- package/lib/types/subdocument.js +2 -2
- package/lib/virtualtype.js +4 -3
- package/package.json +18 -17
- package/tools/repl.js +8 -8
- package/tools/sharded.js +3 -3
- package/types/connection.d.ts +116 -116
- package/types/document.d.ts +3 -0
- package/types/error.d.ts +2 -2
- package/types/index.d.ts +75 -67
- package/types/pipelinestage.d.ts +194 -194
- package/types/schemaoptions.d.ts +2 -2
package/lib/model.js
CHANGED
|
@@ -82,7 +82,7 @@ const saveToObjectOptions = Object.assign({}, internalToObjectOptions, {
|
|
|
82
82
|
* [`connection.model()`](./api.html#connection_Connection-model) functions
|
|
83
83
|
* create subclasses of `mongoose.Model` as shown below.
|
|
84
84
|
*
|
|
85
|
-
* ####Example:
|
|
85
|
+
* #### Example:
|
|
86
86
|
*
|
|
87
87
|
* // `UserModel` is a "Model", a subclass of `mongoose.Model`.
|
|
88
88
|
* const UserModel = mongoose.model('User', new Schema({ name: String }));
|
|
@@ -201,12 +201,12 @@ Model.prototype.baseModelName;
|
|
|
201
201
|
* Event emitter that reports any errors that occurred. Useful for global error
|
|
202
202
|
* handling.
|
|
203
203
|
*
|
|
204
|
-
* ####Example:
|
|
204
|
+
* #### Example:
|
|
205
205
|
*
|
|
206
206
|
* MyModel.events.on('error', err => console.log(err.message));
|
|
207
207
|
*
|
|
208
208
|
* // Prints a 'CastError' because of the above handler
|
|
209
|
-
* await MyModel.findOne({ _id: '
|
|
209
|
+
* await MyModel.findOne({ _id: 'Not a valid ObjectId' }).catch(noop);
|
|
210
210
|
*
|
|
211
211
|
* @api public
|
|
212
212
|
* @fires error whenever any query or model function errors
|
|
@@ -245,8 +245,7 @@ function _applyCustomWhere(doc, where) {
|
|
|
245
245
|
*/
|
|
246
246
|
|
|
247
247
|
Model.prototype.$__handleSave = function(options, callback) {
|
|
248
|
-
const
|
|
249
|
-
let saveOptions = {};
|
|
248
|
+
const saveOptions = {};
|
|
250
249
|
|
|
251
250
|
applyWriteConcern(this.$__schema, options);
|
|
252
251
|
if (typeof options.writeConcern !== 'undefined') {
|
|
@@ -274,14 +273,10 @@ Model.prototype.$__handleSave = function(options, callback) {
|
|
|
274
273
|
if ('checkKeys' in options) {
|
|
275
274
|
saveOptions.checkKeys = options.checkKeys;
|
|
276
275
|
}
|
|
277
|
-
const session = this.$session();
|
|
278
276
|
if (!saveOptions.hasOwnProperty('session')) {
|
|
279
|
-
saveOptions.session = session;
|
|
277
|
+
saveOptions.session = this.$session();
|
|
280
278
|
}
|
|
281
279
|
|
|
282
|
-
if (Object.keys(saveOptions).length === 0) {
|
|
283
|
-
saveOptions = null;
|
|
284
|
-
}
|
|
285
280
|
if (this.$isNew) {
|
|
286
281
|
// send entire doc
|
|
287
282
|
const obj = this.toObject(saveToObjectOptions);
|
|
@@ -298,9 +293,9 @@ Model.prototype.$__handleSave = function(options, callback) {
|
|
|
298
293
|
}
|
|
299
294
|
|
|
300
295
|
this.$__version(true, obj);
|
|
301
|
-
this[modelCollectionSymbol].insertOne(obj, saveOptions,
|
|
296
|
+
this[modelCollectionSymbol].insertOne(obj, saveOptions, (err, ret) => {
|
|
302
297
|
if (err) {
|
|
303
|
-
_setIsNew(
|
|
298
|
+
_setIsNew(this, true);
|
|
304
299
|
|
|
305
300
|
callback(err, null);
|
|
306
301
|
return;
|
|
@@ -308,64 +303,68 @@ Model.prototype.$__handleSave = function(options, callback) {
|
|
|
308
303
|
|
|
309
304
|
callback(null, ret);
|
|
310
305
|
});
|
|
306
|
+
|
|
311
307
|
this.$__reset();
|
|
312
308
|
_setIsNew(this, false);
|
|
313
309
|
// Make it possible to retry the insert
|
|
314
310
|
this.$__.inserting = true;
|
|
315
|
-
} else {
|
|
316
|
-
// Make sure we don't treat it as a new object on error,
|
|
317
|
-
// since it already exists
|
|
318
|
-
this.$__.inserting = false;
|
|
319
|
-
|
|
320
|
-
const delta = this.$__delta();
|
|
321
|
-
if (delta) {
|
|
322
|
-
if (delta instanceof MongooseError) {
|
|
323
|
-
callback(delta);
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
311
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
callback(where);
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
332
314
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
this.$__undoReset();
|
|
315
|
+
// Make sure we don't treat it as a new object on error,
|
|
316
|
+
// since it already exists
|
|
317
|
+
this.$__.inserting = false;
|
|
337
318
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
callback(null, ret);
|
|
343
|
-
});
|
|
344
|
-
} else {
|
|
345
|
-
const optionsWithCustomValues = Object.assign({}, options, saveOptions);
|
|
346
|
-
const where = this.$__where();
|
|
347
|
-
if (this.$__schema.options.optimisticConcurrency) {
|
|
348
|
-
const key = this.$__schema.options.versionKey;
|
|
349
|
-
const val = this.$__getValue(key);
|
|
350
|
-
if (val != null) {
|
|
351
|
-
where[key] = val;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
this.constructor.exists(where, optionsWithCustomValues)
|
|
355
|
-
.then(documentExists => {
|
|
356
|
-
const matchedCount = !documentExists ? 0 : 1;
|
|
357
|
-
callback(null, { $where: where, matchedCount });
|
|
358
|
-
})
|
|
359
|
-
.catch(callback);
|
|
319
|
+
const delta = this.$__delta();
|
|
320
|
+
if (delta) {
|
|
321
|
+
if (delta instanceof MongooseError) {
|
|
322
|
+
callback(delta);
|
|
360
323
|
return;
|
|
361
324
|
}
|
|
362
325
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
326
|
+
const where = this.$__where(delta[0]);
|
|
327
|
+
if (where instanceof MongooseError) {
|
|
328
|
+
callback(where);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
366
331
|
|
|
367
|
-
|
|
332
|
+
_applyCustomWhere(this, where);
|
|
333
|
+
this[modelCollectionSymbol].updateOne(where, delta[1], saveOptions, (err, ret) => {
|
|
334
|
+
if (err) {
|
|
335
|
+
this.$__undoReset();
|
|
336
|
+
|
|
337
|
+
callback(err);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
ret.$where = where;
|
|
341
|
+
callback(null, ret);
|
|
342
|
+
});
|
|
343
|
+
} else {
|
|
344
|
+
const optionsWithCustomValues = Object.assign({}, options, saveOptions);
|
|
345
|
+
const where = this.$__where();
|
|
346
|
+
const optimisticConcurrency = this.$__schema.options.optimisticConcurrency;
|
|
347
|
+
if (optimisticConcurrency) {
|
|
348
|
+
const key = this.$__schema.options.versionKey;
|
|
349
|
+
const val = this.$__getValue(key);
|
|
350
|
+
if (val != null) {
|
|
351
|
+
where[key] = val;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
this.constructor.exists(where, optionsWithCustomValues)
|
|
355
|
+
.then(documentExists => {
|
|
356
|
+
const matchedCount = !documentExists ? 0 : 1;
|
|
357
|
+
callback(null, { $where: where, matchedCount });
|
|
358
|
+
})
|
|
359
|
+
.catch(callback);
|
|
360
|
+
return;
|
|
368
361
|
}
|
|
362
|
+
|
|
363
|
+
// store the modified paths before the document is reset
|
|
364
|
+
this.$__.modifiedPaths = this.modifiedPaths();
|
|
365
|
+
this.$__reset();
|
|
366
|
+
|
|
367
|
+
_setIsNew(this, false);
|
|
369
368
|
};
|
|
370
369
|
|
|
371
370
|
/*!
|
|
@@ -374,8 +373,8 @@ Model.prototype.$__handleSave = function(options, callback) {
|
|
|
374
373
|
|
|
375
374
|
Model.prototype.$__save = function(options, callback) {
|
|
376
375
|
this.$__handleSave(options, (error, result) => {
|
|
377
|
-
const hooks = this.$__schema.s.hooks;
|
|
378
376
|
if (error) {
|
|
377
|
+
const hooks = this.$__schema.s.hooks;
|
|
379
378
|
return hooks.execPost('save:error', this, [this], { error: error }, (error) => {
|
|
380
379
|
callback(error, this);
|
|
381
380
|
});
|
|
@@ -399,7 +398,7 @@ Model.prototype.$__save = function(options, callback) {
|
|
|
399
398
|
}
|
|
400
399
|
}
|
|
401
400
|
|
|
402
|
-
const versionBump = this.$__.version
|
|
401
|
+
const versionBump = this.$__.version;
|
|
403
402
|
// was this an update that required a version bump?
|
|
404
403
|
if (versionBump && !this.$__.inserting) {
|
|
405
404
|
const doIncrement = VERSION_INC === (VERSION_INC & this.$__.version);
|
|
@@ -423,6 +422,7 @@ Model.prototype.$__save = function(options, callback) {
|
|
|
423
422
|
this.$__undoReset();
|
|
424
423
|
error = new DocumentNotFoundError(result.$where,
|
|
425
424
|
this.constructor.modelName, numAffected, result);
|
|
425
|
+
const hooks = this.$__schema.s.hooks;
|
|
426
426
|
return hooks.execPost('save:error', this, [this], { error: error }, (error) => {
|
|
427
427
|
callback(error, this);
|
|
428
428
|
});
|
|
@@ -453,7 +453,7 @@ function generateVersionError(doc, modifiedPaths) {
|
|
|
453
453
|
* Saves this document by inserting a new document into the database if [document.isNew](/docs/api.html#document_Document-isNew) is `true`,
|
|
454
454
|
* or sends an [updateOne](/docs/api.html#document_Document-updateOne) operation with just the modified paths if `isNew` is `false`.
|
|
455
455
|
*
|
|
456
|
-
* ####Example:
|
|
456
|
+
* #### Example:
|
|
457
457
|
*
|
|
458
458
|
* product.sold = Date.now();
|
|
459
459
|
* product = await product.save();
|
|
@@ -461,7 +461,7 @@ function generateVersionError(doc, modifiedPaths) {
|
|
|
461
461
|
* If save is successful, the returned promise will fulfill with the document
|
|
462
462
|
* saved.
|
|
463
463
|
*
|
|
464
|
-
* ####Example:
|
|
464
|
+
* #### Example:
|
|
465
465
|
*
|
|
466
466
|
* const newProduct = await product.save();
|
|
467
467
|
* newProduct === product; // true
|
|
@@ -516,9 +516,9 @@ Model.prototype.save = function(options, fn) {
|
|
|
516
516
|
this.$__.saveOptions = options;
|
|
517
517
|
|
|
518
518
|
this.$__save(options, error => {
|
|
519
|
-
this.$__.saving =
|
|
520
|
-
|
|
521
|
-
|
|
519
|
+
this.$__.saving = null;
|
|
520
|
+
this.$__.saveOptions = null;
|
|
521
|
+
this.$__.$versionError = null;
|
|
522
522
|
this.$op = null;
|
|
523
523
|
|
|
524
524
|
if (error) {
|
|
@@ -576,7 +576,6 @@ function operand(self, where, delta, data, val, op) {
|
|
|
576
576
|
if (VERSION_ALL === (VERSION_ALL & self.$__.version)) return;
|
|
577
577
|
|
|
578
578
|
if (self.$__schema.options.optimisticConcurrency) {
|
|
579
|
-
self.$__.version = VERSION_ALL;
|
|
580
579
|
return;
|
|
581
580
|
}
|
|
582
581
|
|
|
@@ -699,6 +698,12 @@ function handleAtomics(self, where, delta, data, value) {
|
|
|
699
698
|
|
|
700
699
|
Model.prototype.$__delta = function() {
|
|
701
700
|
const dirty = this.$__dirty();
|
|
701
|
+
|
|
702
|
+
const optimisticConcurrency = this.$__schema.options.optimisticConcurrency;
|
|
703
|
+
if (optimisticConcurrency) {
|
|
704
|
+
this.$__.version = dirty.length ? VERSION_ALL : VERSION_WHERE;
|
|
705
|
+
}
|
|
706
|
+
|
|
702
707
|
if (!dirty.length && VERSION_ALL !== this.$__.version) {
|
|
703
708
|
return;
|
|
704
709
|
}
|
|
@@ -884,7 +889,7 @@ Model.prototype.$__version = function(where, delta) {
|
|
|
884
889
|
/**
|
|
885
890
|
* Signal that we desire an increment of this documents version.
|
|
886
891
|
*
|
|
887
|
-
* ####Example:
|
|
892
|
+
* #### Example:
|
|
888
893
|
*
|
|
889
894
|
* Model.findById(id, function (err, doc) {
|
|
890
895
|
* doc.increment();
|
|
@@ -928,7 +933,7 @@ Model.prototype.$__where = function _where(where) {
|
|
|
928
933
|
/**
|
|
929
934
|
* Removes this document from the db.
|
|
930
935
|
*
|
|
931
|
-
* ####Example:
|
|
936
|
+
* #### Example:
|
|
932
937
|
* product.remove(function (err, product) {
|
|
933
938
|
* if (err) return handleError(err);
|
|
934
939
|
* Product.findById(product._id, function (err, product) {
|
|
@@ -939,7 +944,7 @@ Model.prototype.$__where = function _where(where) {
|
|
|
939
944
|
*
|
|
940
945
|
* As an extra measure of flow control, remove will return a Promise (bound to `fn` if passed) so it could be chained, or hooked to receive errors
|
|
941
946
|
*
|
|
942
|
-
* ####Example:
|
|
947
|
+
* #### Example:
|
|
943
948
|
* product.remove().then(function (product) {
|
|
944
949
|
* ...
|
|
945
950
|
* }).catch(function (err) {
|
|
@@ -986,7 +991,7 @@ Model.prototype.delete = Model.prototype.remove;
|
|
|
986
991
|
/**
|
|
987
992
|
* Removes this document from the db. Equivalent to `.remove()`.
|
|
988
993
|
*
|
|
989
|
-
* ####Example:
|
|
994
|
+
* #### Example:
|
|
990
995
|
* product = await product.deleteOne();
|
|
991
996
|
* await Product.findById(product._id); // null
|
|
992
997
|
*
|
|
@@ -1055,7 +1060,7 @@ Model.prototype.$__deleteOne = Model.prototype.$__remove;
|
|
|
1055
1060
|
/**
|
|
1056
1061
|
* Returns another Model instance.
|
|
1057
1062
|
*
|
|
1058
|
-
* ####Example:
|
|
1063
|
+
* #### Example:
|
|
1059
1064
|
*
|
|
1060
1065
|
* const doc = new Tank;
|
|
1061
1066
|
* doc.model('User').findById(id, callback);
|
|
@@ -1075,7 +1080,7 @@ Model.prototype.model = function model(name) {
|
|
|
1075
1080
|
* Under the hood, `MyModel.exists({ answer: 42 })` is equivalent to
|
|
1076
1081
|
* `MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean()`
|
|
1077
1082
|
*
|
|
1078
|
-
* ####Example:
|
|
1083
|
+
* #### Example:
|
|
1079
1084
|
* await Character.deleteMany({});
|
|
1080
1085
|
* await Character.create({ name: 'Jean-Luc Picard' });
|
|
1081
1086
|
*
|
|
@@ -1114,7 +1119,7 @@ Model.exists = function exists(filter, options, callback) {
|
|
|
1114
1119
|
/**
|
|
1115
1120
|
* Adds a discriminator type.
|
|
1116
1121
|
*
|
|
1117
|
-
* ####Example:
|
|
1122
|
+
* #### Example:
|
|
1118
1123
|
*
|
|
1119
1124
|
* function BaseSchema() {
|
|
1120
1125
|
* Schema.apply(this, arguments);
|
|
@@ -1243,7 +1248,7 @@ for (const i in EventEmitter.prototype) {
|
|
|
1243
1248
|
* to get back a promise that will resolve when your indexes are finished
|
|
1244
1249
|
* building as an alternative to [`MyModel.on('index')`](/docs/guide.html#indexes)
|
|
1245
1250
|
*
|
|
1246
|
-
* ####Example:
|
|
1251
|
+
* #### Example:
|
|
1247
1252
|
*
|
|
1248
1253
|
* const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
|
|
1249
1254
|
* // This calls `Event.init()` implicitly, so you don't need to call
|
|
@@ -1329,7 +1334,7 @@ Model.init = function init(callback) {
|
|
|
1329
1334
|
* Note 2: You don't have to call this if your schema contains index or unique field.
|
|
1330
1335
|
* In that case, just use `Model.init()`
|
|
1331
1336
|
*
|
|
1332
|
-
* ####Example:
|
|
1337
|
+
* #### Example:
|
|
1333
1338
|
*
|
|
1334
1339
|
* const userSchema = new Schema({ name: String })
|
|
1335
1340
|
* const User = mongoose.model('User', userSchema);
|
|
@@ -1411,7 +1416,7 @@ Model.createCollection = function createCollection(options, callback) {
|
|
|
1411
1416
|
* See the [introductory blog post](https://thecodebarbarian.com/whats-new-in-mongoose-5-2-syncindexes)
|
|
1412
1417
|
* for more information.
|
|
1413
1418
|
*
|
|
1414
|
-
* ####Example:
|
|
1419
|
+
* #### Example:
|
|
1415
1420
|
*
|
|
1416
1421
|
* const schema = new Schema({ name: { type: String, unique: true } });
|
|
1417
1422
|
* const Customer = mongoose.model('Customer', schema);
|
|
@@ -1629,7 +1634,7 @@ Model.listIndexes = function init(callback) {
|
|
|
1629
1634
|
* Sends `createIndex` commands to mongo for each index declared in the schema.
|
|
1630
1635
|
* The `createIndex` commands are sent in series.
|
|
1631
1636
|
*
|
|
1632
|
-
* ####Example:
|
|
1637
|
+
* #### Example:
|
|
1633
1638
|
*
|
|
1634
1639
|
* Event.ensureIndexes(function (err) {
|
|
1635
1640
|
* if (err) return handleError(err);
|
|
@@ -1637,7 +1642,7 @@ Model.listIndexes = function init(callback) {
|
|
|
1637
1642
|
*
|
|
1638
1643
|
* After completion, an `index` event is emitted on this `Model` passing an error if one occurred.
|
|
1639
1644
|
*
|
|
1640
|
-
* ####Example:
|
|
1645
|
+
* #### Example:
|
|
1641
1646
|
*
|
|
1642
1647
|
* const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
|
|
1643
1648
|
* const Event = mongoose.model('Event', eventSchema);
|
|
@@ -1874,7 +1879,7 @@ Model.discriminators;
|
|
|
1874
1879
|
/**
|
|
1875
1880
|
* Translate any aliases fields/conditions so the final query or document object is pure
|
|
1876
1881
|
*
|
|
1877
|
-
* ####Example:
|
|
1882
|
+
* #### Example:
|
|
1878
1883
|
*
|
|
1879
1884
|
* Character
|
|
1880
1885
|
* .find(Character.translateAliases({
|
|
@@ -1882,10 +1887,10 @@ Model.discriminators;
|
|
|
1882
1887
|
* })
|
|
1883
1888
|
* .exec(function(err, characters) {})
|
|
1884
1889
|
*
|
|
1885
|
-
* ####Note:
|
|
1890
|
+
* #### Note:
|
|
1886
1891
|
* Only translate arguments of object type anything else is returned raw
|
|
1887
1892
|
*
|
|
1888
|
-
* @param {Object}
|
|
1893
|
+
* @param {Object} fields fields/conditions that may contain aliased keys
|
|
1889
1894
|
* @return {Object} the translated 'pure' fields/conditions
|
|
1890
1895
|
*/
|
|
1891
1896
|
Model.translateAliases = function translateAliases(fields) {
|
|
@@ -1971,12 +1976,12 @@ Model.translateAliases = function translateAliases(fields) {
|
|
|
1971
1976
|
*
|
|
1972
1977
|
* This method is deprecated. See [Deprecation Warnings](../deprecations.html#remove) for details.
|
|
1973
1978
|
*
|
|
1974
|
-
* ####Example:
|
|
1979
|
+
* #### Example:
|
|
1975
1980
|
*
|
|
1976
1981
|
* const res = await Character.remove({ name: 'Eddard Stark' });
|
|
1977
1982
|
* res.deletedCount; // Number of documents removed
|
|
1978
1983
|
*
|
|
1979
|
-
* ####Note:
|
|
1984
|
+
* #### Note:
|
|
1980
1985
|
*
|
|
1981
1986
|
* This method sends a remove command directly to MongoDB, no Mongoose documents
|
|
1982
1987
|
* are involved. Because no Mongoose documents are involved, Mongoose does
|
|
@@ -2018,11 +2023,11 @@ Model.remove = function remove(conditions, options, callback) {
|
|
|
2018
2023
|
* Behaves like `remove()`, but deletes at most one document regardless of the
|
|
2019
2024
|
* `single` option.
|
|
2020
2025
|
*
|
|
2021
|
-
* ####Example:
|
|
2026
|
+
* #### Example:
|
|
2022
2027
|
*
|
|
2023
2028
|
* await Character.deleteOne({ name: 'Eddard Stark' }); // returns {deletedCount: 1}
|
|
2024
2029
|
*
|
|
2025
|
-
* ####Note:
|
|
2030
|
+
* #### Note:
|
|
2026
2031
|
*
|
|
2027
2032
|
* This function triggers `deleteOne` query hooks. Read the
|
|
2028
2033
|
* [middleware docs](/docs/middleware.html#naming) to learn more.
|
|
@@ -2061,11 +2066,11 @@ Model.deleteOne = function deleteOne(conditions, options, callback) {
|
|
|
2061
2066
|
* Behaves like `remove()`, but deletes all documents that match `conditions`
|
|
2062
2067
|
* regardless of the `single` option.
|
|
2063
2068
|
*
|
|
2064
|
-
* ####Example:
|
|
2069
|
+
* #### Example:
|
|
2065
2070
|
*
|
|
2066
2071
|
* await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }); // returns {deletedCount: x} where x is the number of documents deleted.
|
|
2067
2072
|
*
|
|
2068
|
-
* ####Note:
|
|
2073
|
+
* #### Note:
|
|
2069
2074
|
*
|
|
2070
2075
|
* This function triggers `deleteMany` query hooks. Read the
|
|
2071
2076
|
* [middleware docs](/docs/middleware.html#naming) to learn more.
|
|
@@ -2104,7 +2109,7 @@ Model.deleteMany = function deleteMany(conditions, options, callback) {
|
|
|
2104
2109
|
* See our [query casting tutorial](/docs/tutorials/query_casting.html) for
|
|
2105
2110
|
* more information on how Mongoose casts `filter`.
|
|
2106
2111
|
*
|
|
2107
|
-
* ####Examples:
|
|
2112
|
+
* #### Examples:
|
|
2108
2113
|
*
|
|
2109
2114
|
* // find all documents
|
|
2110
2115
|
* await MyModel.find({});
|
|
@@ -2180,7 +2185,7 @@ Model.find = function find(conditions, projection, options, callback) {
|
|
|
2180
2185
|
* to `findOne({})` and return arbitrary documents. However, mongoose
|
|
2181
2186
|
* translates `findById(undefined)` into `findOne({ _id: null })`.
|
|
2182
2187
|
*
|
|
2183
|
-
* ####Example:
|
|
2188
|
+
* #### Example:
|
|
2184
2189
|
*
|
|
2185
2190
|
* // Find the adventure with the given `id`, or `null` if not found
|
|
2186
2191
|
* await Adventure.findById(id).exec();
|
|
@@ -2223,7 +2228,7 @@ Model.findById = function findById(id, projection, options, callback) {
|
|
|
2223
2228
|
* mongoose will send an empty `findOne` command to MongoDB, which will return
|
|
2224
2229
|
* an arbitrary document. If you're querying by `_id`, use `findById()` instead.
|
|
2225
2230
|
*
|
|
2226
|
-
* ####Example:
|
|
2231
|
+
* #### Example:
|
|
2227
2232
|
*
|
|
2228
2233
|
* // Find one adventure whose `country` is 'Croatia', otherwise `null`
|
|
2229
2234
|
* await Adventure.findOne({ country: 'Croatia' }).exec();
|
|
@@ -2279,7 +2284,7 @@ Model.findOne = function findOne(conditions, projection, options, callback) {
|
|
|
2279
2284
|
* `estimatedDocumentCount()` uses collection metadata rather than scanning
|
|
2280
2285
|
* the entire collection.
|
|
2281
2286
|
*
|
|
2282
|
-
* ####Example:
|
|
2287
|
+
* #### Example:
|
|
2283
2288
|
*
|
|
2284
2289
|
* const numAdventures = await Adventure.estimatedDocumentCount();
|
|
2285
2290
|
*
|
|
@@ -2302,7 +2307,7 @@ Model.estimatedDocumentCount = function estimatedDocumentCount(options, callback
|
|
|
2302
2307
|
/**
|
|
2303
2308
|
* Counts number of documents matching `filter` in a database collection.
|
|
2304
2309
|
*
|
|
2305
|
-
* ####Example:
|
|
2310
|
+
* #### Example:
|
|
2306
2311
|
*
|
|
2307
2312
|
* Adventure.countDocuments({ type: 'jungle' }, function (err, count) {
|
|
2308
2313
|
* console.log('there are %d jungle adventures', count);
|
|
@@ -2357,7 +2362,7 @@ Model.countDocuments = function countDocuments(conditions, options, callback) {
|
|
|
2357
2362
|
* a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model.estimatedDocumentCount)
|
|
2358
2363
|
* instead. Otherwise, use the [`countDocuments()`](/docs/api.html#model_Model.countDocuments) function instead.
|
|
2359
2364
|
*
|
|
2360
|
-
* ####Example:
|
|
2365
|
+
* #### Example:
|
|
2361
2366
|
*
|
|
2362
2367
|
* const count = await Adventure.count({ type: 'jungle' });
|
|
2363
2368
|
* console.log('there are %d jungle adventures', count);
|
|
@@ -2389,7 +2394,7 @@ Model.count = function count(conditions, callback) {
|
|
|
2389
2394
|
*
|
|
2390
2395
|
* Passing a `callback` executes the query.
|
|
2391
2396
|
*
|
|
2392
|
-
* ####Example
|
|
2397
|
+
* #### Example
|
|
2393
2398
|
*
|
|
2394
2399
|
* Link.distinct('url', { clicks: {$gt: 100}}, function (err, result) {
|
|
2395
2400
|
* if (err) return handleError(err);
|
|
@@ -2481,7 +2486,7 @@ Model.$where = function $where() {
|
|
|
2481
2486
|
*
|
|
2482
2487
|
* Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found document (if any) to the callback. The query executes if `callback` is passed else a Query object is returned.
|
|
2483
2488
|
*
|
|
2484
|
-
* ####Options:
|
|
2489
|
+
* #### Options:
|
|
2485
2490
|
*
|
|
2486
2491
|
* - `new`: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
|
|
2487
2492
|
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
@@ -2494,7 +2499,7 @@ Model.$where = function $where() {
|
|
|
2494
2499
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2495
2500
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2496
2501
|
*
|
|
2497
|
-
* ####Examples:
|
|
2502
|
+
* #### Examples:
|
|
2498
2503
|
*
|
|
2499
2504
|
* A.findOneAndUpdate(conditions, update, options, callback) // executes
|
|
2500
2505
|
* A.findOneAndUpdate(conditions, update, options) // returns Query
|
|
@@ -2502,11 +2507,11 @@ Model.$where = function $where() {
|
|
|
2502
2507
|
* A.findOneAndUpdate(conditions, update) // returns Query
|
|
2503
2508
|
* A.findOneAndUpdate() // returns Query
|
|
2504
2509
|
*
|
|
2505
|
-
* ####Note:
|
|
2510
|
+
* #### Note:
|
|
2506
2511
|
*
|
|
2507
2512
|
* All top level update keys which are not `atomic` operation names are treated as set operations:
|
|
2508
2513
|
*
|
|
2509
|
-
* ####Example:
|
|
2514
|
+
* #### Example:
|
|
2510
2515
|
*
|
|
2511
2516
|
* const query = { name: 'borne' };
|
|
2512
2517
|
* Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)
|
|
@@ -2516,7 +2521,7 @@ Model.$where = function $where() {
|
|
|
2516
2521
|
*
|
|
2517
2522
|
* This helps prevent accidentally overwriting your document with `{ name: 'jason bourne' }`.
|
|
2518
2523
|
*
|
|
2519
|
-
* ####Note:
|
|
2524
|
+
* #### Note:
|
|
2520
2525
|
*
|
|
2521
2526
|
* `findOneAndX` and `findByIdAndX` functions support limited validation that
|
|
2522
2527
|
* you can enable by setting the `runValidators` option.
|
|
@@ -2619,7 +2624,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2619
2624
|
*
|
|
2620
2625
|
* - `findOneAndUpdate()`
|
|
2621
2626
|
*
|
|
2622
|
-
* ####Options:
|
|
2627
|
+
* #### Options:
|
|
2623
2628
|
*
|
|
2624
2629
|
* - `new`: bool - true to return the modified document rather than the original. defaults to false
|
|
2625
2630
|
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
@@ -2630,7 +2635,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2630
2635
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2631
2636
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2632
2637
|
*
|
|
2633
|
-
* ####Examples:
|
|
2638
|
+
* #### Examples:
|
|
2634
2639
|
*
|
|
2635
2640
|
* A.findByIdAndUpdate(id, update, options, callback) // executes
|
|
2636
2641
|
* A.findByIdAndUpdate(id, update, options) // returns Query
|
|
@@ -2638,11 +2643,11 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2638
2643
|
* A.findByIdAndUpdate(id, update) // returns Query
|
|
2639
2644
|
* A.findByIdAndUpdate() // returns Query
|
|
2640
2645
|
*
|
|
2641
|
-
* ####Note:
|
|
2646
|
+
* #### Note:
|
|
2642
2647
|
*
|
|
2643
2648
|
* All top level update keys which are not `atomic` operation names are treated as set operations:
|
|
2644
2649
|
*
|
|
2645
|
-
* ####Example:
|
|
2650
|
+
* #### Example:
|
|
2646
2651
|
*
|
|
2647
2652
|
* Model.findByIdAndUpdate(id, { name: 'jason bourne' }, options, callback)
|
|
2648
2653
|
*
|
|
@@ -2651,7 +2656,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2651
2656
|
*
|
|
2652
2657
|
* This helps prevent accidentally overwriting your document with `{ name: 'jason bourne' }`.
|
|
2653
2658
|
*
|
|
2654
|
-
* ####Note:
|
|
2659
|
+
* #### Note:
|
|
2655
2660
|
*
|
|
2656
2661
|
* `findOneAndX` and `findByIdAndX` functions support limited validation. You can
|
|
2657
2662
|
* enable validation by setting the `runValidators` option.
|
|
@@ -2722,7 +2727,7 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
|
|
|
2722
2727
|
* this distinction is purely pedantic. You should use `findOneAndDelete()`
|
|
2723
2728
|
* unless you have a good reason not to.
|
|
2724
2729
|
*
|
|
2725
|
-
* ####Options:
|
|
2730
|
+
* #### Options:
|
|
2726
2731
|
*
|
|
2727
2732
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2728
2733
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
@@ -2731,7 +2736,7 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
|
|
|
2731
2736
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2732
2737
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2733
2738
|
*
|
|
2734
|
-
* ####Examples:
|
|
2739
|
+
* #### Examples:
|
|
2735
2740
|
*
|
|
2736
2741
|
* A.findOneAndDelete(conditions, options, callback) // executes
|
|
2737
2742
|
* A.findOneAndDelete(conditions, options) // return Query
|
|
@@ -2835,7 +2840,7 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
|
|
2835
2840
|
*
|
|
2836
2841
|
* - `findOneAndReplace()`
|
|
2837
2842
|
*
|
|
2838
|
-
* ####Options:
|
|
2843
|
+
* #### Options:
|
|
2839
2844
|
*
|
|
2840
2845
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2841
2846
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
@@ -2844,7 +2849,7 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
|
|
2844
2849
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2845
2850
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2846
2851
|
*
|
|
2847
|
-
* ####Examples:
|
|
2852
|
+
* #### Examples:
|
|
2848
2853
|
*
|
|
2849
2854
|
* A.findOneAndReplace(filter, replacement, options, callback) // executes
|
|
2850
2855
|
* A.findOneAndReplace(filter, replacement, options) // return Query
|
|
@@ -2914,7 +2919,7 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
|
|
|
2914
2919
|
*
|
|
2915
2920
|
* - `findOneAndRemove()`
|
|
2916
2921
|
*
|
|
2917
|
-
* ####Options:
|
|
2922
|
+
* #### Options:
|
|
2918
2923
|
*
|
|
2919
2924
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2920
2925
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
@@ -2923,7 +2928,7 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
|
|
|
2923
2928
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2924
2929
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2925
2930
|
*
|
|
2926
|
-
* ####Examples:
|
|
2931
|
+
* #### Examples:
|
|
2927
2932
|
*
|
|
2928
2933
|
* A.findOneAndRemove(conditions, options, callback) // executes
|
|
2929
2934
|
* A.findOneAndRemove(conditions, options) // return Query
|
|
@@ -2994,14 +2999,14 @@ Model.findOneAndRemove = function(conditions, options, callback) {
|
|
|
2994
2999
|
*
|
|
2995
3000
|
* - `findOneAndRemove()`
|
|
2996
3001
|
*
|
|
2997
|
-
* ####Options:
|
|
3002
|
+
* #### Options:
|
|
2998
3003
|
*
|
|
2999
3004
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
3000
3005
|
* - `select`: sets the document fields to return
|
|
3001
3006
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
3002
3007
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
3003
3008
|
*
|
|
3004
|
-
* ####Examples:
|
|
3009
|
+
* #### Examples:
|
|
3005
3010
|
*
|
|
3006
3011
|
* A.findByIdAndRemove(id, options, callback) // executes
|
|
3007
3012
|
* A.findByIdAndRemove(id, options) // return Query
|
|
@@ -3044,7 +3049,7 @@ Model.findByIdAndRemove = function(id, options, callback) {
|
|
|
3044
3049
|
*
|
|
3045
3050
|
* - `save()`
|
|
3046
3051
|
*
|
|
3047
|
-
* ####Example:
|
|
3052
|
+
* #### Example:
|
|
3048
3053
|
*
|
|
3049
3054
|
* // Insert one new `Character` document
|
|
3050
3055
|
* await Character.create({ name: 'Jean-Luc Picard' });
|
|
@@ -3196,7 +3201,7 @@ Model.create = function create(doc, options, callback) {
|
|
|
3196
3201
|
* - 'end': Emitted if the underlying stream is closed
|
|
3197
3202
|
* - 'close': Emitted if the underlying stream is closed
|
|
3198
3203
|
*
|
|
3199
|
-
* ####Example:
|
|
3204
|
+
* #### Example:
|
|
3200
3205
|
*
|
|
3201
3206
|
* const doc = await Person.create({ name: 'Ned Stark' });
|
|
3202
3207
|
* const changeStream = Person.watch().on('change', change => console.log(change));
|
|
@@ -3245,7 +3250,7 @@ Model.watch = function(pipeline, options) {
|
|
|
3245
3250
|
*
|
|
3246
3251
|
* This function does not trigger any middleware.
|
|
3247
3252
|
*
|
|
3248
|
-
* ####Example:
|
|
3253
|
+
* #### Example:
|
|
3249
3254
|
*
|
|
3250
3255
|
* const session = await Person.startSession();
|
|
3251
3256
|
* let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
|
|
@@ -3286,7 +3291,7 @@ Model.startSession = function() {
|
|
|
3286
3291
|
*
|
|
3287
3292
|
* - `insertMany()`
|
|
3288
3293
|
*
|
|
3289
|
-
* ####Example:
|
|
3294
|
+
* #### Example:
|
|
3290
3295
|
*
|
|
3291
3296
|
* const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
|
|
3292
3297
|
* Movies.insertMany(arr, function(error, docs) {});
|
|
@@ -3506,7 +3511,7 @@ function _setIsNew(doc, val) {
|
|
|
3506
3511
|
* If you need to trigger
|
|
3507
3512
|
* `save()` middleware for every document use [`create()`](https://mongoosejs.com/docs/api.html#model_Model.create) instead.
|
|
3508
3513
|
*
|
|
3509
|
-
* ####Example:
|
|
3514
|
+
* #### Example:
|
|
3510
3515
|
*
|
|
3511
3516
|
* Character.bulkWrite([
|
|
3512
3517
|
* {
|
|
@@ -3762,7 +3767,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
|
|
|
3762
3767
|
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
|
|
3763
3768
|
* The document returned has no paths marked as modified initially.
|
|
3764
3769
|
*
|
|
3765
|
-
* ####Example:
|
|
3770
|
+
* #### Example:
|
|
3766
3771
|
*
|
|
3767
3772
|
* // hydrate previous data into a Mongoose document
|
|
3768
3773
|
* const mongooseCandy = Candy.hydrate({ _id: '54108337212ffb6d459f854c', type: 'jelly bean' });
|
|
@@ -3797,7 +3802,7 @@ Model.hydrate = function(obj, projection) {
|
|
|
3797
3802
|
*
|
|
3798
3803
|
* This method is deprecated. See [Deprecation Warnings](../deprecations.html#update) for details.
|
|
3799
3804
|
*
|
|
3800
|
-
* ####Examples:
|
|
3805
|
+
* #### Examples:
|
|
3801
3806
|
*
|
|
3802
3807
|
* MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
|
|
3803
3808
|
*
|
|
@@ -3807,7 +3812,7 @@ Model.hydrate = function(obj, projection) {
|
|
|
3807
3812
|
* // had `ferret` set to `true`, `nModified` will be 0.
|
|
3808
3813
|
* res.nModified;
|
|
3809
3814
|
*
|
|
3810
|
-
* ####Valid options:
|
|
3815
|
+
* #### Valid options:
|
|
3811
3816
|
*
|
|
3812
3817
|
* - `strict` (boolean): overrides the [schema-level `strict` option](/docs/guide.html#strict) for this update
|
|
3813
3818
|
* - `upsert` (boolean): whether to create the doc if it doesn't match (false)
|
|
@@ -3825,11 +3830,11 @@ Model.hydrate = function(obj, projection) {
|
|
|
3825
3830
|
* - `err` is the error if any occurred
|
|
3826
3831
|
* - `rawResponse` is the full response from Mongo
|
|
3827
3832
|
*
|
|
3828
|
-
* ####Note:
|
|
3833
|
+
* #### Note:
|
|
3829
3834
|
*
|
|
3830
3835
|
* All top level keys which are not `atomic` operation names are treated as set operations:
|
|
3831
3836
|
*
|
|
3832
|
-
* ####Example:
|
|
3837
|
+
* #### Example:
|
|
3833
3838
|
*
|
|
3834
3839
|
* const query = { name: 'borne' };
|
|
3835
3840
|
* Model.update(query, { name: 'jason bourne' }, options, callback);
|
|
@@ -3840,7 +3845,7 @@ Model.hydrate = function(obj, projection) {
|
|
|
3840
3845
|
*
|
|
3841
3846
|
* This helps prevent accidentally overwriting all documents in your collection with `{ name: 'jason bourne' }`.
|
|
3842
3847
|
*
|
|
3843
|
-
* ####Note:
|
|
3848
|
+
* #### Note:
|
|
3844
3849
|
*
|
|
3845
3850
|
* Be careful to not use an existing model instance for the update clause (this won't work and can cause weird behavior like infinite loops). Also, ensure that the update clause does not have an _id property, which causes Mongo to return a "Mod on _id not allowed" error.
|
|
3846
3851
|
*
|
|
@@ -3881,7 +3886,7 @@ Model.update = function update(conditions, doc, options, callback) {
|
|
|
3881
3886
|
* **Note** updateMany will _not_ fire update middleware. Use `pre('updateMany')`
|
|
3882
3887
|
* and `post('updateMany')` instead.
|
|
3883
3888
|
*
|
|
3884
|
-
* ####Example:
|
|
3889
|
+
* #### Example:
|
|
3885
3890
|
* const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
|
|
3886
3891
|
* res.matchedCount; // Number of documents matched
|
|
3887
3892
|
* res.modifiedCount; // Number of documents modified
|
|
@@ -3921,7 +3926,7 @@ Model.updateMany = function updateMany(conditions, doc, options, callback) {
|
|
|
3921
3926
|
* - MongoDB will update _only_ the first document that matches `filter` regardless of the value of the `multi` option.
|
|
3922
3927
|
* - Use `replaceOne()` if you want to overwrite an entire document rather than using atomic operators like `$set`.
|
|
3923
3928
|
*
|
|
3924
|
-
* ####Example:
|
|
3929
|
+
* #### Example:
|
|
3925
3930
|
* const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
|
|
3926
3931
|
* res.matchedCount; // Number of documents matched
|
|
3927
3932
|
* res.modifiedCount; // Number of documents modified
|
|
@@ -3958,7 +3963,7 @@ Model.updateOne = function updateOne(conditions, doc, options, callback) {
|
|
|
3958
3963
|
* Same as `update()`, except MongoDB replace the existing document with the
|
|
3959
3964
|
* given document (no atomic operators like `$set`).
|
|
3960
3965
|
*
|
|
3961
|
-
* ####Example:
|
|
3966
|
+
* #### Example:
|
|
3962
3967
|
* const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
|
|
3963
3968
|
* res.matchedCount; // Number of documents matched
|
|
3964
3969
|
* res.modifiedCount; // Number of documents modified
|
|
@@ -4029,7 +4034,7 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4029
4034
|
*
|
|
4030
4035
|
* This function does not trigger any middleware.
|
|
4031
4036
|
*
|
|
4032
|
-
* ####Example:
|
|
4037
|
+
* #### Example:
|
|
4033
4038
|
*
|
|
4034
4039
|
* const o = {};
|
|
4035
4040
|
* // `map()` and `reduce()` are run on the MongoDB server, not Node.js,
|
|
@@ -4040,7 +4045,7 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4040
4045
|
* console.log(results)
|
|
4041
4046
|
* })
|
|
4042
4047
|
*
|
|
4043
|
-
* ####Other options:
|
|
4048
|
+
* #### Other options:
|
|
4044
4049
|
*
|
|
4045
4050
|
* - `query` {Object} query filter object.
|
|
4046
4051
|
* - `sort` {Object} sort input objects using this key
|
|
@@ -4053,7 +4058,7 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4053
4058
|
* - `readPreference` {String}
|
|
4054
4059
|
* - `out*` {Object, default: {inline:1}} sets the output target for the map reduce job.
|
|
4055
4060
|
*
|
|
4056
|
-
*
|
|
4061
|
+
* #### * out options:
|
|
4057
4062
|
*
|
|
4058
4063
|
* - `{inline:1}` the results are returned in an array
|
|
4059
4064
|
* - `{replace: 'collectionName'}` add the results to collectionName: the results replace the collection
|
|
@@ -4062,7 +4067,7 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4062
4067
|
*
|
|
4063
4068
|
* If `options.out` is set to `replace`, `merge`, or `reduce`, a Model instance is returned that can be used for further querying. Queries run against this model are all executed with the [`lean` option](/docs/tutorials/lean.html); meaning only the js object is returned and no Mongoose magic is applied (getters, setters, etc).
|
|
4064
4069
|
*
|
|
4065
|
-
* ####Example:
|
|
4070
|
+
* #### Example:
|
|
4066
4071
|
*
|
|
4067
4072
|
* const o = {};
|
|
4068
4073
|
* // You can also define `map()` and `reduce()` as strings if your
|
|
@@ -4155,7 +4160,7 @@ Model.mapReduce = function mapReduce(o, callback) {
|
|
|
4155
4160
|
*
|
|
4156
4161
|
* - `aggregate()`
|
|
4157
4162
|
*
|
|
4158
|
-
* ####Example:
|
|
4163
|
+
* #### Example:
|
|
4159
4164
|
*
|
|
4160
4165
|
* // Find the max balance of all accounts
|
|
4161
4166
|
* const res = await Users.aggregate([
|
|
@@ -4172,7 +4177,7 @@ Model.mapReduce = function mapReduce(o, callback) {
|
|
|
4172
4177
|
* exec();
|
|
4173
4178
|
* console.log(res); // [ { maxBalance: 98 } ]
|
|
4174
4179
|
*
|
|
4175
|
-
* ####
|
|
4180
|
+
* #### Note:
|
|
4176
4181
|
*
|
|
4177
4182
|
* - Mongoose does **not** cast aggregation pipelines to the model's schema because `$project` and `$group` operators allow redefining the "shape" of the documents at any stage of the pipeline, which may leave documents in an incompatible format. You can use the [mongoose-cast-aggregation plugin](https://github.com/AbdelrahmanHafez/mongoose-cast-aggregation) to enable minimal casting for aggregation pipelines.
|
|
4178
4183
|
* - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
|
|
@@ -4234,7 +4239,7 @@ Model.aggregate = function aggregate(pipeline, options, callback) {
|
|
|
4234
4239
|
* Casts and validates the given object against this model's schema, passing the
|
|
4235
4240
|
* given `context` to custom validators.
|
|
4236
4241
|
*
|
|
4237
|
-
* ####Example:
|
|
4242
|
+
* #### Example:
|
|
4238
4243
|
*
|
|
4239
4244
|
* const Model = mongoose.model('Test', Schema({
|
|
4240
4245
|
* name: { type: String, required: true },
|
|
@@ -4368,7 +4373,7 @@ Model.validate = function validate(obj, pathsToValidate, context, callback) {
|
|
|
4368
4373
|
* Changed in Mongoose 6: the model you call `populate()` on should be the
|
|
4369
4374
|
* "local field" model, **not** the "foreign field" model.
|
|
4370
4375
|
*
|
|
4371
|
-
* ####Available top-level options:
|
|
4376
|
+
* #### Available top-level options:
|
|
4372
4377
|
*
|
|
4373
4378
|
* - path: space delimited path(s) to populate
|
|
4374
4379
|
* - select: optional fields to select
|
|
@@ -4378,7 +4383,7 @@ Model.validate = function validate(obj, pathsToValidate, context, callback) {
|
|
|
4378
4383
|
* - justOne: optional boolean, if true Mongoose will always set `path` to an array. Inferred from schema by default.
|
|
4379
4384
|
* - strictPopulate: optional boolean, set to `false` to allow populating paths that aren't in the schema.
|
|
4380
4385
|
*
|
|
4381
|
-
* ####Examples:
|
|
4386
|
+
* #### Examples:
|
|
4382
4387
|
*
|
|
4383
4388
|
* const Dog = mongoose.model('Dog', new Schema({ name: String, breed: String }));
|
|
4384
4389
|
* const Person = mongoose.model('Person', new Schema({
|
|
@@ -5028,7 +5033,7 @@ Model.$wrapCallback = function(callback) {
|
|
|
5028
5033
|
* Helper for console.log. Given a model named 'MyModel', returns the string
|
|
5029
5034
|
* `'Model { MyModel }'`.
|
|
5030
5035
|
*
|
|
5031
|
-
* ####Example:
|
|
5036
|
+
* #### Example:
|
|
5032
5037
|
*
|
|
5033
5038
|
* const MyModel = mongoose.model('Test', Schema({ name: String }));
|
|
5034
5039
|
* MyModel.inspect(); // 'Model { Test }'
|