mongoose 6.2.9 → 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/CHANGELOG.md +6 -0
- package/dist/browser.umd.js +2 -2
- package/lib/aggregate.js +36 -36
- package/lib/browser.js +4 -4
- package/lib/connection.js +21 -21
- package/lib/cursor/AggregationCursor.js +2 -2
- package/lib/cursor/QueryCursor.js +3 -3
- package/lib/document.js +36 -36
- package/lib/error/index.js +2 -2
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/index.js +23 -23
- package/lib/model.js +83 -77
- package/lib/options/SchemaArrayOptions.js +2 -2
- package/lib/options/SchemaBufferOptions.js +1 -1
- package/lib/options/SchemaDateOptions.js +2 -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 +214 -223
- package/lib/schema/SubdocumentPath.js +2 -2
- package/lib/schema/array.js +2 -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 +3 -3
- 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 +75 -67
- 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 +12 -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 +3 -3
- package/package.json +10 -10
- package/types/index.d.ts +5 -3
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
|
|
@@ -343,7 +343,8 @@ Model.prototype.$__handleSave = function(options, callback) {
|
|
|
343
343
|
} else {
|
|
344
344
|
const optionsWithCustomValues = Object.assign({}, options, saveOptions);
|
|
345
345
|
const where = this.$__where();
|
|
346
|
-
|
|
346
|
+
const optimisticConcurrency = this.$__schema.options.optimisticConcurrency;
|
|
347
|
+
if (optimisticConcurrency) {
|
|
347
348
|
const key = this.$__schema.options.versionKey;
|
|
348
349
|
const val = this.$__getValue(key);
|
|
349
350
|
if (val != null) {
|
|
@@ -397,7 +398,7 @@ Model.prototype.$__save = function(options, callback) {
|
|
|
397
398
|
}
|
|
398
399
|
}
|
|
399
400
|
|
|
400
|
-
const versionBump = this.$__.version
|
|
401
|
+
const versionBump = this.$__.version;
|
|
401
402
|
// was this an update that required a version bump?
|
|
402
403
|
if (versionBump && !this.$__.inserting) {
|
|
403
404
|
const doIncrement = VERSION_INC === (VERSION_INC & this.$__.version);
|
|
@@ -452,7 +453,7 @@ function generateVersionError(doc, modifiedPaths) {
|
|
|
452
453
|
* Saves this document by inserting a new document into the database if [document.isNew](/docs/api.html#document_Document-isNew) is `true`,
|
|
453
454
|
* or sends an [updateOne](/docs/api.html#document_Document-updateOne) operation with just the modified paths if `isNew` is `false`.
|
|
454
455
|
*
|
|
455
|
-
* ####Example:
|
|
456
|
+
* #### Example:
|
|
456
457
|
*
|
|
457
458
|
* product.sold = Date.now();
|
|
458
459
|
* product = await product.save();
|
|
@@ -460,7 +461,7 @@ function generateVersionError(doc, modifiedPaths) {
|
|
|
460
461
|
* If save is successful, the returned promise will fulfill with the document
|
|
461
462
|
* saved.
|
|
462
463
|
*
|
|
463
|
-
* ####Example:
|
|
464
|
+
* #### Example:
|
|
464
465
|
*
|
|
465
466
|
* const newProduct = await product.save();
|
|
466
467
|
* newProduct === product; // true
|
|
@@ -575,7 +576,6 @@ function operand(self, where, delta, data, val, op) {
|
|
|
575
576
|
if (VERSION_ALL === (VERSION_ALL & self.$__.version)) return;
|
|
576
577
|
|
|
577
578
|
if (self.$__schema.options.optimisticConcurrency) {
|
|
578
|
-
self.$__.version = VERSION_ALL;
|
|
579
579
|
return;
|
|
580
580
|
}
|
|
581
581
|
|
|
@@ -698,6 +698,12 @@ function handleAtomics(self, where, delta, data, value) {
|
|
|
698
698
|
|
|
699
699
|
Model.prototype.$__delta = function() {
|
|
700
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
|
+
|
|
701
707
|
if (!dirty.length && VERSION_ALL !== this.$__.version) {
|
|
702
708
|
return;
|
|
703
709
|
}
|
|
@@ -883,7 +889,7 @@ Model.prototype.$__version = function(where, delta) {
|
|
|
883
889
|
/**
|
|
884
890
|
* Signal that we desire an increment of this documents version.
|
|
885
891
|
*
|
|
886
|
-
* ####Example:
|
|
892
|
+
* #### Example:
|
|
887
893
|
*
|
|
888
894
|
* Model.findById(id, function (err, doc) {
|
|
889
895
|
* doc.increment();
|
|
@@ -927,7 +933,7 @@ Model.prototype.$__where = function _where(where) {
|
|
|
927
933
|
/**
|
|
928
934
|
* Removes this document from the db.
|
|
929
935
|
*
|
|
930
|
-
* ####Example:
|
|
936
|
+
* #### Example:
|
|
931
937
|
* product.remove(function (err, product) {
|
|
932
938
|
* if (err) return handleError(err);
|
|
933
939
|
* Product.findById(product._id, function (err, product) {
|
|
@@ -938,7 +944,7 @@ Model.prototype.$__where = function _where(where) {
|
|
|
938
944
|
*
|
|
939
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
|
|
940
946
|
*
|
|
941
|
-
* ####Example:
|
|
947
|
+
* #### Example:
|
|
942
948
|
* product.remove().then(function (product) {
|
|
943
949
|
* ...
|
|
944
950
|
* }).catch(function (err) {
|
|
@@ -985,7 +991,7 @@ Model.prototype.delete = Model.prototype.remove;
|
|
|
985
991
|
/**
|
|
986
992
|
* Removes this document from the db. Equivalent to `.remove()`.
|
|
987
993
|
*
|
|
988
|
-
* ####Example:
|
|
994
|
+
* #### Example:
|
|
989
995
|
* product = await product.deleteOne();
|
|
990
996
|
* await Product.findById(product._id); // null
|
|
991
997
|
*
|
|
@@ -1054,7 +1060,7 @@ Model.prototype.$__deleteOne = Model.prototype.$__remove;
|
|
|
1054
1060
|
/**
|
|
1055
1061
|
* Returns another Model instance.
|
|
1056
1062
|
*
|
|
1057
|
-
* ####Example:
|
|
1063
|
+
* #### Example:
|
|
1058
1064
|
*
|
|
1059
1065
|
* const doc = new Tank;
|
|
1060
1066
|
* doc.model('User').findById(id, callback);
|
|
@@ -1074,7 +1080,7 @@ Model.prototype.model = function model(name) {
|
|
|
1074
1080
|
* Under the hood, `MyModel.exists({ answer: 42 })` is equivalent to
|
|
1075
1081
|
* `MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean()`
|
|
1076
1082
|
*
|
|
1077
|
-
* ####Example:
|
|
1083
|
+
* #### Example:
|
|
1078
1084
|
* await Character.deleteMany({});
|
|
1079
1085
|
* await Character.create({ name: 'Jean-Luc Picard' });
|
|
1080
1086
|
*
|
|
@@ -1113,7 +1119,7 @@ Model.exists = function exists(filter, options, callback) {
|
|
|
1113
1119
|
/**
|
|
1114
1120
|
* Adds a discriminator type.
|
|
1115
1121
|
*
|
|
1116
|
-
* ####Example:
|
|
1122
|
+
* #### Example:
|
|
1117
1123
|
*
|
|
1118
1124
|
* function BaseSchema() {
|
|
1119
1125
|
* Schema.apply(this, arguments);
|
|
@@ -1242,7 +1248,7 @@ for (const i in EventEmitter.prototype) {
|
|
|
1242
1248
|
* to get back a promise that will resolve when your indexes are finished
|
|
1243
1249
|
* building as an alternative to [`MyModel.on('index')`](/docs/guide.html#indexes)
|
|
1244
1250
|
*
|
|
1245
|
-
* ####Example:
|
|
1251
|
+
* #### Example:
|
|
1246
1252
|
*
|
|
1247
1253
|
* const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
|
|
1248
1254
|
* // This calls `Event.init()` implicitly, so you don't need to call
|
|
@@ -1328,7 +1334,7 @@ Model.init = function init(callback) {
|
|
|
1328
1334
|
* Note 2: You don't have to call this if your schema contains index or unique field.
|
|
1329
1335
|
* In that case, just use `Model.init()`
|
|
1330
1336
|
*
|
|
1331
|
-
* ####Example:
|
|
1337
|
+
* #### Example:
|
|
1332
1338
|
*
|
|
1333
1339
|
* const userSchema = new Schema({ name: String })
|
|
1334
1340
|
* const User = mongoose.model('User', userSchema);
|
|
@@ -1410,7 +1416,7 @@ Model.createCollection = function createCollection(options, callback) {
|
|
|
1410
1416
|
* See the [introductory blog post](https://thecodebarbarian.com/whats-new-in-mongoose-5-2-syncindexes)
|
|
1411
1417
|
* for more information.
|
|
1412
1418
|
*
|
|
1413
|
-
* ####Example:
|
|
1419
|
+
* #### Example:
|
|
1414
1420
|
*
|
|
1415
1421
|
* const schema = new Schema({ name: { type: String, unique: true } });
|
|
1416
1422
|
* const Customer = mongoose.model('Customer', schema);
|
|
@@ -1628,7 +1634,7 @@ Model.listIndexes = function init(callback) {
|
|
|
1628
1634
|
* Sends `createIndex` commands to mongo for each index declared in the schema.
|
|
1629
1635
|
* The `createIndex` commands are sent in series.
|
|
1630
1636
|
*
|
|
1631
|
-
* ####Example:
|
|
1637
|
+
* #### Example:
|
|
1632
1638
|
*
|
|
1633
1639
|
* Event.ensureIndexes(function (err) {
|
|
1634
1640
|
* if (err) return handleError(err);
|
|
@@ -1636,7 +1642,7 @@ Model.listIndexes = function init(callback) {
|
|
|
1636
1642
|
*
|
|
1637
1643
|
* After completion, an `index` event is emitted on this `Model` passing an error if one occurred.
|
|
1638
1644
|
*
|
|
1639
|
-
* ####Example:
|
|
1645
|
+
* #### Example:
|
|
1640
1646
|
*
|
|
1641
1647
|
* const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
|
|
1642
1648
|
* const Event = mongoose.model('Event', eventSchema);
|
|
@@ -1873,7 +1879,7 @@ Model.discriminators;
|
|
|
1873
1879
|
/**
|
|
1874
1880
|
* Translate any aliases fields/conditions so the final query or document object is pure
|
|
1875
1881
|
*
|
|
1876
|
-
* ####Example:
|
|
1882
|
+
* #### Example:
|
|
1877
1883
|
*
|
|
1878
1884
|
* Character
|
|
1879
1885
|
* .find(Character.translateAliases({
|
|
@@ -1881,7 +1887,7 @@ Model.discriminators;
|
|
|
1881
1887
|
* })
|
|
1882
1888
|
* .exec(function(err, characters) {})
|
|
1883
1889
|
*
|
|
1884
|
-
* ####Note:
|
|
1890
|
+
* #### Note:
|
|
1885
1891
|
* Only translate arguments of object type anything else is returned raw
|
|
1886
1892
|
*
|
|
1887
1893
|
* @param {Object} fields fields/conditions that may contain aliased keys
|
|
@@ -1970,12 +1976,12 @@ Model.translateAliases = function translateAliases(fields) {
|
|
|
1970
1976
|
*
|
|
1971
1977
|
* This method is deprecated. See [Deprecation Warnings](../deprecations.html#remove) for details.
|
|
1972
1978
|
*
|
|
1973
|
-
* ####Example:
|
|
1979
|
+
* #### Example:
|
|
1974
1980
|
*
|
|
1975
1981
|
* const res = await Character.remove({ name: 'Eddard Stark' });
|
|
1976
1982
|
* res.deletedCount; // Number of documents removed
|
|
1977
1983
|
*
|
|
1978
|
-
* ####Note:
|
|
1984
|
+
* #### Note:
|
|
1979
1985
|
*
|
|
1980
1986
|
* This method sends a remove command directly to MongoDB, no Mongoose documents
|
|
1981
1987
|
* are involved. Because no Mongoose documents are involved, Mongoose does
|
|
@@ -2017,11 +2023,11 @@ Model.remove = function remove(conditions, options, callback) {
|
|
|
2017
2023
|
* Behaves like `remove()`, but deletes at most one document regardless of the
|
|
2018
2024
|
* `single` option.
|
|
2019
2025
|
*
|
|
2020
|
-
* ####Example:
|
|
2026
|
+
* #### Example:
|
|
2021
2027
|
*
|
|
2022
2028
|
* await Character.deleteOne({ name: 'Eddard Stark' }); // returns {deletedCount: 1}
|
|
2023
2029
|
*
|
|
2024
|
-
* ####Note:
|
|
2030
|
+
* #### Note:
|
|
2025
2031
|
*
|
|
2026
2032
|
* This function triggers `deleteOne` query hooks. Read the
|
|
2027
2033
|
* [middleware docs](/docs/middleware.html#naming) to learn more.
|
|
@@ -2060,11 +2066,11 @@ Model.deleteOne = function deleteOne(conditions, options, callback) {
|
|
|
2060
2066
|
* Behaves like `remove()`, but deletes all documents that match `conditions`
|
|
2061
2067
|
* regardless of the `single` option.
|
|
2062
2068
|
*
|
|
2063
|
-
* ####Example:
|
|
2069
|
+
* #### Example:
|
|
2064
2070
|
*
|
|
2065
2071
|
* await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }); // returns {deletedCount: x} where x is the number of documents deleted.
|
|
2066
2072
|
*
|
|
2067
|
-
* ####Note:
|
|
2073
|
+
* #### Note:
|
|
2068
2074
|
*
|
|
2069
2075
|
* This function triggers `deleteMany` query hooks. Read the
|
|
2070
2076
|
* [middleware docs](/docs/middleware.html#naming) to learn more.
|
|
@@ -2103,7 +2109,7 @@ Model.deleteMany = function deleteMany(conditions, options, callback) {
|
|
|
2103
2109
|
* See our [query casting tutorial](/docs/tutorials/query_casting.html) for
|
|
2104
2110
|
* more information on how Mongoose casts `filter`.
|
|
2105
2111
|
*
|
|
2106
|
-
* ####Examples:
|
|
2112
|
+
* #### Examples:
|
|
2107
2113
|
*
|
|
2108
2114
|
* // find all documents
|
|
2109
2115
|
* await MyModel.find({});
|
|
@@ -2179,7 +2185,7 @@ Model.find = function find(conditions, projection, options, callback) {
|
|
|
2179
2185
|
* to `findOne({})` and return arbitrary documents. However, mongoose
|
|
2180
2186
|
* translates `findById(undefined)` into `findOne({ _id: null })`.
|
|
2181
2187
|
*
|
|
2182
|
-
* ####Example:
|
|
2188
|
+
* #### Example:
|
|
2183
2189
|
*
|
|
2184
2190
|
* // Find the adventure with the given `id`, or `null` if not found
|
|
2185
2191
|
* await Adventure.findById(id).exec();
|
|
@@ -2222,7 +2228,7 @@ Model.findById = function findById(id, projection, options, callback) {
|
|
|
2222
2228
|
* mongoose will send an empty `findOne` command to MongoDB, which will return
|
|
2223
2229
|
* an arbitrary document. If you're querying by `_id`, use `findById()` instead.
|
|
2224
2230
|
*
|
|
2225
|
-
* ####Example:
|
|
2231
|
+
* #### Example:
|
|
2226
2232
|
*
|
|
2227
2233
|
* // Find one adventure whose `country` is 'Croatia', otherwise `null`
|
|
2228
2234
|
* await Adventure.findOne({ country: 'Croatia' }).exec();
|
|
@@ -2278,7 +2284,7 @@ Model.findOne = function findOne(conditions, projection, options, callback) {
|
|
|
2278
2284
|
* `estimatedDocumentCount()` uses collection metadata rather than scanning
|
|
2279
2285
|
* the entire collection.
|
|
2280
2286
|
*
|
|
2281
|
-
* ####Example:
|
|
2287
|
+
* #### Example:
|
|
2282
2288
|
*
|
|
2283
2289
|
* const numAdventures = await Adventure.estimatedDocumentCount();
|
|
2284
2290
|
*
|
|
@@ -2301,7 +2307,7 @@ Model.estimatedDocumentCount = function estimatedDocumentCount(options, callback
|
|
|
2301
2307
|
/**
|
|
2302
2308
|
* Counts number of documents matching `filter` in a database collection.
|
|
2303
2309
|
*
|
|
2304
|
-
* ####Example:
|
|
2310
|
+
* #### Example:
|
|
2305
2311
|
*
|
|
2306
2312
|
* Adventure.countDocuments({ type: 'jungle' }, function (err, count) {
|
|
2307
2313
|
* console.log('there are %d jungle adventures', count);
|
|
@@ -2356,7 +2362,7 @@ Model.countDocuments = function countDocuments(conditions, options, callback) {
|
|
|
2356
2362
|
* a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model.estimatedDocumentCount)
|
|
2357
2363
|
* instead. Otherwise, use the [`countDocuments()`](/docs/api.html#model_Model.countDocuments) function instead.
|
|
2358
2364
|
*
|
|
2359
|
-
* ####Example:
|
|
2365
|
+
* #### Example:
|
|
2360
2366
|
*
|
|
2361
2367
|
* const count = await Adventure.count({ type: 'jungle' });
|
|
2362
2368
|
* console.log('there are %d jungle adventures', count);
|
|
@@ -2388,7 +2394,7 @@ Model.count = function count(conditions, callback) {
|
|
|
2388
2394
|
*
|
|
2389
2395
|
* Passing a `callback` executes the query.
|
|
2390
2396
|
*
|
|
2391
|
-
* ####Example
|
|
2397
|
+
* #### Example
|
|
2392
2398
|
*
|
|
2393
2399
|
* Link.distinct('url', { clicks: {$gt: 100}}, function (err, result) {
|
|
2394
2400
|
* if (err) return handleError(err);
|
|
@@ -2480,7 +2486,7 @@ Model.$where = function $where() {
|
|
|
2480
2486
|
*
|
|
2481
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.
|
|
2482
2488
|
*
|
|
2483
|
-
* ####Options:
|
|
2489
|
+
* #### Options:
|
|
2484
2490
|
*
|
|
2485
2491
|
* - `new`: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
|
|
2486
2492
|
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
@@ -2493,7 +2499,7 @@ Model.$where = function $where() {
|
|
|
2493
2499
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2494
2500
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2495
2501
|
*
|
|
2496
|
-
* ####Examples:
|
|
2502
|
+
* #### Examples:
|
|
2497
2503
|
*
|
|
2498
2504
|
* A.findOneAndUpdate(conditions, update, options, callback) // executes
|
|
2499
2505
|
* A.findOneAndUpdate(conditions, update, options) // returns Query
|
|
@@ -2501,11 +2507,11 @@ Model.$where = function $where() {
|
|
|
2501
2507
|
* A.findOneAndUpdate(conditions, update) // returns Query
|
|
2502
2508
|
* A.findOneAndUpdate() // returns Query
|
|
2503
2509
|
*
|
|
2504
|
-
* ####Note:
|
|
2510
|
+
* #### Note:
|
|
2505
2511
|
*
|
|
2506
2512
|
* All top level update keys which are not `atomic` operation names are treated as set operations:
|
|
2507
2513
|
*
|
|
2508
|
-
* ####Example:
|
|
2514
|
+
* #### Example:
|
|
2509
2515
|
*
|
|
2510
2516
|
* const query = { name: 'borne' };
|
|
2511
2517
|
* Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)
|
|
@@ -2515,7 +2521,7 @@ Model.$where = function $where() {
|
|
|
2515
2521
|
*
|
|
2516
2522
|
* This helps prevent accidentally overwriting your document with `{ name: 'jason bourne' }`.
|
|
2517
2523
|
*
|
|
2518
|
-
* ####Note:
|
|
2524
|
+
* #### Note:
|
|
2519
2525
|
*
|
|
2520
2526
|
* `findOneAndX` and `findByIdAndX` functions support limited validation that
|
|
2521
2527
|
* you can enable by setting the `runValidators` option.
|
|
@@ -2618,7 +2624,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2618
2624
|
*
|
|
2619
2625
|
* - `findOneAndUpdate()`
|
|
2620
2626
|
*
|
|
2621
|
-
* ####Options:
|
|
2627
|
+
* #### Options:
|
|
2622
2628
|
*
|
|
2623
2629
|
* - `new`: bool - true to return the modified document rather than the original. defaults to false
|
|
2624
2630
|
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
@@ -2629,7 +2635,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2629
2635
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2630
2636
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2631
2637
|
*
|
|
2632
|
-
* ####Examples:
|
|
2638
|
+
* #### Examples:
|
|
2633
2639
|
*
|
|
2634
2640
|
* A.findByIdAndUpdate(id, update, options, callback) // executes
|
|
2635
2641
|
* A.findByIdAndUpdate(id, update, options) // returns Query
|
|
@@ -2637,11 +2643,11 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2637
2643
|
* A.findByIdAndUpdate(id, update) // returns Query
|
|
2638
2644
|
* A.findByIdAndUpdate() // returns Query
|
|
2639
2645
|
*
|
|
2640
|
-
* ####Note:
|
|
2646
|
+
* #### Note:
|
|
2641
2647
|
*
|
|
2642
2648
|
* All top level update keys which are not `atomic` operation names are treated as set operations:
|
|
2643
2649
|
*
|
|
2644
|
-
* ####Example:
|
|
2650
|
+
* #### Example:
|
|
2645
2651
|
*
|
|
2646
2652
|
* Model.findByIdAndUpdate(id, { name: 'jason bourne' }, options, callback)
|
|
2647
2653
|
*
|
|
@@ -2650,7 +2656,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2650
2656
|
*
|
|
2651
2657
|
* This helps prevent accidentally overwriting your document with `{ name: 'jason bourne' }`.
|
|
2652
2658
|
*
|
|
2653
|
-
* ####Note:
|
|
2659
|
+
* #### Note:
|
|
2654
2660
|
*
|
|
2655
2661
|
* `findOneAndX` and `findByIdAndX` functions support limited validation. You can
|
|
2656
2662
|
* enable validation by setting the `runValidators` option.
|
|
@@ -2721,7 +2727,7 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
|
|
|
2721
2727
|
* this distinction is purely pedantic. You should use `findOneAndDelete()`
|
|
2722
2728
|
* unless you have a good reason not to.
|
|
2723
2729
|
*
|
|
2724
|
-
* ####Options:
|
|
2730
|
+
* #### Options:
|
|
2725
2731
|
*
|
|
2726
2732
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2727
2733
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
@@ -2730,7 +2736,7 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
|
|
|
2730
2736
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2731
2737
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2732
2738
|
*
|
|
2733
|
-
* ####Examples:
|
|
2739
|
+
* #### Examples:
|
|
2734
2740
|
*
|
|
2735
2741
|
* A.findOneAndDelete(conditions, options, callback) // executes
|
|
2736
2742
|
* A.findOneAndDelete(conditions, options) // return Query
|
|
@@ -2834,7 +2840,7 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
|
|
2834
2840
|
*
|
|
2835
2841
|
* - `findOneAndReplace()`
|
|
2836
2842
|
*
|
|
2837
|
-
* ####Options:
|
|
2843
|
+
* #### Options:
|
|
2838
2844
|
*
|
|
2839
2845
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2840
2846
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
@@ -2843,7 +2849,7 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
|
|
2843
2849
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2844
2850
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2845
2851
|
*
|
|
2846
|
-
* ####Examples:
|
|
2852
|
+
* #### Examples:
|
|
2847
2853
|
*
|
|
2848
2854
|
* A.findOneAndReplace(filter, replacement, options, callback) // executes
|
|
2849
2855
|
* A.findOneAndReplace(filter, replacement, options) // return Query
|
|
@@ -2913,7 +2919,7 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
|
|
|
2913
2919
|
*
|
|
2914
2920
|
* - `findOneAndRemove()`
|
|
2915
2921
|
*
|
|
2916
|
-
* ####Options:
|
|
2922
|
+
* #### Options:
|
|
2917
2923
|
*
|
|
2918
2924
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2919
2925
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
@@ -2922,7 +2928,7 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
|
|
|
2922
2928
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2923
2929
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2924
2930
|
*
|
|
2925
|
-
* ####Examples:
|
|
2931
|
+
* #### Examples:
|
|
2926
2932
|
*
|
|
2927
2933
|
* A.findOneAndRemove(conditions, options, callback) // executes
|
|
2928
2934
|
* A.findOneAndRemove(conditions, options) // return Query
|
|
@@ -2993,14 +2999,14 @@ Model.findOneAndRemove = function(conditions, options, callback) {
|
|
|
2993
2999
|
*
|
|
2994
3000
|
* - `findOneAndRemove()`
|
|
2995
3001
|
*
|
|
2996
|
-
* ####Options:
|
|
3002
|
+
* #### Options:
|
|
2997
3003
|
*
|
|
2998
3004
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2999
3005
|
* - `select`: sets the document fields to return
|
|
3000
3006
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
3001
3007
|
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
3002
3008
|
*
|
|
3003
|
-
* ####Examples:
|
|
3009
|
+
* #### Examples:
|
|
3004
3010
|
*
|
|
3005
3011
|
* A.findByIdAndRemove(id, options, callback) // executes
|
|
3006
3012
|
* A.findByIdAndRemove(id, options) // return Query
|
|
@@ -3043,7 +3049,7 @@ Model.findByIdAndRemove = function(id, options, callback) {
|
|
|
3043
3049
|
*
|
|
3044
3050
|
* - `save()`
|
|
3045
3051
|
*
|
|
3046
|
-
* ####Example:
|
|
3052
|
+
* #### Example:
|
|
3047
3053
|
*
|
|
3048
3054
|
* // Insert one new `Character` document
|
|
3049
3055
|
* await Character.create({ name: 'Jean-Luc Picard' });
|
|
@@ -3195,7 +3201,7 @@ Model.create = function create(doc, options, callback) {
|
|
|
3195
3201
|
* - 'end': Emitted if the underlying stream is closed
|
|
3196
3202
|
* - 'close': Emitted if the underlying stream is closed
|
|
3197
3203
|
*
|
|
3198
|
-
* ####Example:
|
|
3204
|
+
* #### Example:
|
|
3199
3205
|
*
|
|
3200
3206
|
* const doc = await Person.create({ name: 'Ned Stark' });
|
|
3201
3207
|
* const changeStream = Person.watch().on('change', change => console.log(change));
|
|
@@ -3244,7 +3250,7 @@ Model.watch = function(pipeline, options) {
|
|
|
3244
3250
|
*
|
|
3245
3251
|
* This function does not trigger any middleware.
|
|
3246
3252
|
*
|
|
3247
|
-
* ####Example:
|
|
3253
|
+
* #### Example:
|
|
3248
3254
|
*
|
|
3249
3255
|
* const session = await Person.startSession();
|
|
3250
3256
|
* let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
|
|
@@ -3285,7 +3291,7 @@ Model.startSession = function() {
|
|
|
3285
3291
|
*
|
|
3286
3292
|
* - `insertMany()`
|
|
3287
3293
|
*
|
|
3288
|
-
* ####Example:
|
|
3294
|
+
* #### Example:
|
|
3289
3295
|
*
|
|
3290
3296
|
* const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
|
|
3291
3297
|
* Movies.insertMany(arr, function(error, docs) {});
|
|
@@ -3505,7 +3511,7 @@ function _setIsNew(doc, val) {
|
|
|
3505
3511
|
* If you need to trigger
|
|
3506
3512
|
* `save()` middleware for every document use [`create()`](https://mongoosejs.com/docs/api.html#model_Model.create) instead.
|
|
3507
3513
|
*
|
|
3508
|
-
* ####Example:
|
|
3514
|
+
* #### Example:
|
|
3509
3515
|
*
|
|
3510
3516
|
* Character.bulkWrite([
|
|
3511
3517
|
* {
|
|
@@ -3761,7 +3767,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
|
|
|
3761
3767
|
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
|
|
3762
3768
|
* The document returned has no paths marked as modified initially.
|
|
3763
3769
|
*
|
|
3764
|
-
* ####Example:
|
|
3770
|
+
* #### Example:
|
|
3765
3771
|
*
|
|
3766
3772
|
* // hydrate previous data into a Mongoose document
|
|
3767
3773
|
* const mongooseCandy = Candy.hydrate({ _id: '54108337212ffb6d459f854c', type: 'jelly bean' });
|
|
@@ -3796,7 +3802,7 @@ Model.hydrate = function(obj, projection) {
|
|
|
3796
3802
|
*
|
|
3797
3803
|
* This method is deprecated. See [Deprecation Warnings](../deprecations.html#update) for details.
|
|
3798
3804
|
*
|
|
3799
|
-
* ####Examples:
|
|
3805
|
+
* #### Examples:
|
|
3800
3806
|
*
|
|
3801
3807
|
* MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
|
|
3802
3808
|
*
|
|
@@ -3806,7 +3812,7 @@ Model.hydrate = function(obj, projection) {
|
|
|
3806
3812
|
* // had `ferret` set to `true`, `nModified` will be 0.
|
|
3807
3813
|
* res.nModified;
|
|
3808
3814
|
*
|
|
3809
|
-
* ####Valid options:
|
|
3815
|
+
* #### Valid options:
|
|
3810
3816
|
*
|
|
3811
3817
|
* - `strict` (boolean): overrides the [schema-level `strict` option](/docs/guide.html#strict) for this update
|
|
3812
3818
|
* - `upsert` (boolean): whether to create the doc if it doesn't match (false)
|
|
@@ -3824,11 +3830,11 @@ Model.hydrate = function(obj, projection) {
|
|
|
3824
3830
|
* - `err` is the error if any occurred
|
|
3825
3831
|
* - `rawResponse` is the full response from Mongo
|
|
3826
3832
|
*
|
|
3827
|
-
* ####Note:
|
|
3833
|
+
* #### Note:
|
|
3828
3834
|
*
|
|
3829
3835
|
* All top level keys which are not `atomic` operation names are treated as set operations:
|
|
3830
3836
|
*
|
|
3831
|
-
* ####Example:
|
|
3837
|
+
* #### Example:
|
|
3832
3838
|
*
|
|
3833
3839
|
* const query = { name: 'borne' };
|
|
3834
3840
|
* Model.update(query, { name: 'jason bourne' }, options, callback);
|
|
@@ -3839,7 +3845,7 @@ Model.hydrate = function(obj, projection) {
|
|
|
3839
3845
|
*
|
|
3840
3846
|
* This helps prevent accidentally overwriting all documents in your collection with `{ name: 'jason bourne' }`.
|
|
3841
3847
|
*
|
|
3842
|
-
* ####Note:
|
|
3848
|
+
* #### Note:
|
|
3843
3849
|
*
|
|
3844
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.
|
|
3845
3851
|
*
|
|
@@ -3880,7 +3886,7 @@ Model.update = function update(conditions, doc, options, callback) {
|
|
|
3880
3886
|
* **Note** updateMany will _not_ fire update middleware. Use `pre('updateMany')`
|
|
3881
3887
|
* and `post('updateMany')` instead.
|
|
3882
3888
|
*
|
|
3883
|
-
* ####Example:
|
|
3889
|
+
* #### Example:
|
|
3884
3890
|
* const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
|
|
3885
3891
|
* res.matchedCount; // Number of documents matched
|
|
3886
3892
|
* res.modifiedCount; // Number of documents modified
|
|
@@ -3920,7 +3926,7 @@ Model.updateMany = function updateMany(conditions, doc, options, callback) {
|
|
|
3920
3926
|
* - MongoDB will update _only_ the first document that matches `filter` regardless of the value of the `multi` option.
|
|
3921
3927
|
* - Use `replaceOne()` if you want to overwrite an entire document rather than using atomic operators like `$set`.
|
|
3922
3928
|
*
|
|
3923
|
-
* ####Example:
|
|
3929
|
+
* #### Example:
|
|
3924
3930
|
* const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
|
|
3925
3931
|
* res.matchedCount; // Number of documents matched
|
|
3926
3932
|
* res.modifiedCount; // Number of documents modified
|
|
@@ -3957,7 +3963,7 @@ Model.updateOne = function updateOne(conditions, doc, options, callback) {
|
|
|
3957
3963
|
* Same as `update()`, except MongoDB replace the existing document with the
|
|
3958
3964
|
* given document (no atomic operators like `$set`).
|
|
3959
3965
|
*
|
|
3960
|
-
* ####Example:
|
|
3966
|
+
* #### Example:
|
|
3961
3967
|
* const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
|
|
3962
3968
|
* res.matchedCount; // Number of documents matched
|
|
3963
3969
|
* res.modifiedCount; // Number of documents modified
|
|
@@ -4028,7 +4034,7 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4028
4034
|
*
|
|
4029
4035
|
* This function does not trigger any middleware.
|
|
4030
4036
|
*
|
|
4031
|
-
* ####Example:
|
|
4037
|
+
* #### Example:
|
|
4032
4038
|
*
|
|
4033
4039
|
* const o = {};
|
|
4034
4040
|
* // `map()` and `reduce()` are run on the MongoDB server, not Node.js,
|
|
@@ -4039,7 +4045,7 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4039
4045
|
* console.log(results)
|
|
4040
4046
|
* })
|
|
4041
4047
|
*
|
|
4042
|
-
* ####Other options:
|
|
4048
|
+
* #### Other options:
|
|
4043
4049
|
*
|
|
4044
4050
|
* - `query` {Object} query filter object.
|
|
4045
4051
|
* - `sort` {Object} sort input objects using this key
|
|
@@ -4052,7 +4058,7 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4052
4058
|
* - `readPreference` {String}
|
|
4053
4059
|
* - `out*` {Object, default: {inline:1}} sets the output target for the map reduce job.
|
|
4054
4060
|
*
|
|
4055
|
-
*
|
|
4061
|
+
* #### * out options:
|
|
4056
4062
|
*
|
|
4057
4063
|
* - `{inline:1}` the results are returned in an array
|
|
4058
4064
|
* - `{replace: 'collectionName'}` add the results to collectionName: the results replace the collection
|
|
@@ -4061,7 +4067,7 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4061
4067
|
*
|
|
4062
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).
|
|
4063
4069
|
*
|
|
4064
|
-
* ####Example:
|
|
4070
|
+
* #### Example:
|
|
4065
4071
|
*
|
|
4066
4072
|
* const o = {};
|
|
4067
4073
|
* // You can also define `map()` and `reduce()` as strings if your
|
|
@@ -4154,7 +4160,7 @@ Model.mapReduce = function mapReduce(o, callback) {
|
|
|
4154
4160
|
*
|
|
4155
4161
|
* - `aggregate()`
|
|
4156
4162
|
*
|
|
4157
|
-
* ####Example:
|
|
4163
|
+
* #### Example:
|
|
4158
4164
|
*
|
|
4159
4165
|
* // Find the max balance of all accounts
|
|
4160
4166
|
* const res = await Users.aggregate([
|
|
@@ -4171,7 +4177,7 @@ Model.mapReduce = function mapReduce(o, callback) {
|
|
|
4171
4177
|
* exec();
|
|
4172
4178
|
* console.log(res); // [ { maxBalance: 98 } ]
|
|
4173
4179
|
*
|
|
4174
|
-
* ####
|
|
4180
|
+
* #### Note:
|
|
4175
4181
|
*
|
|
4176
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.
|
|
4177
4183
|
* - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
|
|
@@ -4233,7 +4239,7 @@ Model.aggregate = function aggregate(pipeline, options, callback) {
|
|
|
4233
4239
|
* Casts and validates the given object against this model's schema, passing the
|
|
4234
4240
|
* given `context` to custom validators.
|
|
4235
4241
|
*
|
|
4236
|
-
* ####Example:
|
|
4242
|
+
* #### Example:
|
|
4237
4243
|
*
|
|
4238
4244
|
* const Model = mongoose.model('Test', Schema({
|
|
4239
4245
|
* name: { type: String, required: true },
|
|
@@ -4367,7 +4373,7 @@ Model.validate = function validate(obj, pathsToValidate, context, callback) {
|
|
|
4367
4373
|
* Changed in Mongoose 6: the model you call `populate()` on should be the
|
|
4368
4374
|
* "local field" model, **not** the "foreign field" model.
|
|
4369
4375
|
*
|
|
4370
|
-
* ####Available top-level options:
|
|
4376
|
+
* #### Available top-level options:
|
|
4371
4377
|
*
|
|
4372
4378
|
* - path: space delimited path(s) to populate
|
|
4373
4379
|
* - select: optional fields to select
|
|
@@ -4377,7 +4383,7 @@ Model.validate = function validate(obj, pathsToValidate, context, callback) {
|
|
|
4377
4383
|
* - justOne: optional boolean, if true Mongoose will always set `path` to an array. Inferred from schema by default.
|
|
4378
4384
|
* - strictPopulate: optional boolean, set to `false` to allow populating paths that aren't in the schema.
|
|
4379
4385
|
*
|
|
4380
|
-
* ####Examples:
|
|
4386
|
+
* #### Examples:
|
|
4381
4387
|
*
|
|
4382
4388
|
* const Dog = mongoose.model('Dog', new Schema({ name: String, breed: String }));
|
|
4383
4389
|
* const Person = mongoose.model('Person', new Schema({
|
|
@@ -5027,7 +5033,7 @@ Model.$wrapCallback = function(callback) {
|
|
|
5027
5033
|
* Helper for console.log. Given a model named 'MyModel', returns the string
|
|
5028
5034
|
* `'Model { MyModel }'`.
|
|
5029
5035
|
*
|
|
5030
|
-
* ####Example:
|
|
5036
|
+
* #### Example:
|
|
5031
5037
|
*
|
|
5032
5038
|
* const MyModel = mongoose.model('Test', Schema({ name: String }));
|
|
5033
5039
|
* MyModel.inspect(); // 'Model { Test }'
|
|
@@ -5,7 +5,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions');
|
|
|
5
5
|
/**
|
|
6
6
|
* The options defined on an Array schematype.
|
|
7
7
|
*
|
|
8
|
-
* ####Example:
|
|
8
|
+
* #### Example:
|
|
9
9
|
*
|
|
10
10
|
* const schema = new Schema({ tags: [String] });
|
|
11
11
|
* schema.path('tags').options; // SchemaArrayOptions instance
|
|
@@ -36,7 +36,7 @@ Object.defineProperty(SchemaArrayOptions.prototype, 'enum', opts);
|
|
|
36
36
|
* If set, specifies the type of this array's values. Equivalent to setting
|
|
37
37
|
* `type` to an array whose first element is `of`.
|
|
38
38
|
*
|
|
39
|
-
* ####Example:
|
|
39
|
+
* #### Example:
|
|
40
40
|
*
|
|
41
41
|
* // `arr` is an array of numbers.
|
|
42
42
|
* new Schema({ arr: [Number] });
|
|
@@ -5,7 +5,7 @@ const SchemaTypeOptions = require('./SchemaTypeOptions');
|
|
|
5
5
|
/**
|
|
6
6
|
* The options defined on a Buffer schematype.
|
|
7
7
|
*
|
|
8
|
-
* ####Example:
|
|
8
|
+
* #### Example:
|
|
9
9
|
*
|
|
10
10
|
* const schema = new Schema({ bitmap: Buffer });
|
|
11
11
|
* schema.path('bitmap').options; // SchemaBufferOptions instance
|