mongoose 6.2.8 → 6.2.11

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.
Files changed (61) hide show
  1. package/.eslintrc.json +35 -26
  2. package/CHANGELOG.md +28 -0
  3. package/dist/browser.umd.js +38 -27
  4. package/lib/aggregate.js +57 -66
  5. package/lib/browser.js +4 -4
  6. package/lib/connection.js +21 -21
  7. package/lib/cursor/AggregationCursor.js +2 -2
  8. package/lib/cursor/QueryCursor.js +3 -3
  9. package/lib/document.js +65 -49
  10. package/lib/error/index.js +2 -2
  11. package/lib/helpers/indexes/applySchemaCollation.js +13 -0
  12. package/lib/helpers/indexes/isTextIndex.js +16 -0
  13. package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
  14. package/lib/helpers/projection/hasIncludedChildren.js +1 -1
  15. package/lib/helpers/query/castUpdate.js +3 -1
  16. package/lib/helpers/update/applyTimestampsToChildren.js +2 -2
  17. package/lib/helpers/update/applyTimestampsToUpdate.js +0 -1
  18. package/lib/index.js +24 -26
  19. package/lib/model.js +144 -146
  20. package/lib/options/SchemaArrayOptions.js +2 -2
  21. package/lib/options/SchemaBufferOptions.js +1 -1
  22. package/lib/options/SchemaDateOptions.js +9 -2
  23. package/lib/options/SchemaDocumentArrayOptions.js +3 -3
  24. package/lib/options/SchemaMapOptions.js +2 -2
  25. package/lib/options/SchemaNumberOptions.js +3 -3
  26. package/lib/options/SchemaObjectIdOptions.js +2 -2
  27. package/lib/options/SchemaStringOptions.js +1 -1
  28. package/lib/options/SchemaSubdocumentOptions.js +2 -2
  29. package/lib/options/SchemaTypeOptions.js +3 -3
  30. package/lib/query.js +291 -248
  31. package/lib/schema/SubdocumentPath.js +4 -3
  32. package/lib/schema/array.js +2 -2
  33. package/lib/schema/boolean.js +4 -4
  34. package/lib/schema/buffer.js +3 -3
  35. package/lib/schema/date.js +7 -7
  36. package/lib/schema/decimal128.js +2 -2
  37. package/lib/schema/documentarray.js +3 -3
  38. package/lib/schema/mixed.js +2 -2
  39. package/lib/schema/number.js +6 -6
  40. package/lib/schema/objectid.js +4 -7
  41. package/lib/schema/string.js +38 -16
  42. package/lib/schema.js +30 -29
  43. package/lib/schematype.js +78 -69
  44. package/lib/types/ArraySubdocument.js +1 -1
  45. package/lib/types/DocumentArray/methods/index.js +2 -2
  46. package/lib/types/array/index.js +1 -1
  47. package/lib/types/array/methods/index.js +13 -13
  48. package/lib/types/buffer.js +1 -1
  49. package/lib/types/decimal128.js +1 -1
  50. package/lib/types/objectid.js +1 -1
  51. package/lib/types/subdocument.js +31 -2
  52. package/lib/virtualtype.js +3 -3
  53. package/package.json +18 -16
  54. package/tools/repl.js +8 -8
  55. package/tools/sharded.js +3 -3
  56. package/types/aggregate.d.ts +223 -0
  57. package/types/connection.d.ts +116 -116
  58. package/types/error.d.ts +2 -2
  59. package/types/index.d.ts +76 -213
  60. package/types/pipelinestage.d.ts +194 -194
  61. package/types/schemaoptions.d.ts +2 -2
package/lib/model.js CHANGED
@@ -26,6 +26,7 @@ const applyQueryMiddleware = require('./helpers/query/applyQueryMiddleware');
26
26
  const applyHooks = require('./helpers/model/applyHooks');
27
27
  const applyMethods = require('./helpers/model/applyMethods');
28
28
  const applyProjection = require('./helpers/projection/applyProjection');
29
+ const applySchemaCollation = require('./helpers/indexes/applySchemaCollation');
29
30
  const applyStaticHooks = require('./helpers/model/applyStaticHooks');
30
31
  const applyStatics = require('./helpers/model/applyStatics');
31
32
  const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
@@ -82,7 +83,7 @@ const saveToObjectOptions = Object.assign({}, internalToObjectOptions, {
82
83
  * [`connection.model()`](./api.html#connection_Connection-model) functions
83
84
  * create subclasses of `mongoose.Model` as shown below.
84
85
  *
85
- * ####Example:
86
+ * #### Example:
86
87
  *
87
88
  * // `UserModel` is a "Model", a subclass of `mongoose.Model`.
88
89
  * const UserModel = mongoose.model('User', new Schema({ name: String }));
@@ -201,12 +202,12 @@ Model.prototype.baseModelName;
201
202
  * Event emitter that reports any errors that occurred. Useful for global error
202
203
  * handling.
203
204
  *
204
- * ####Example:
205
+ * #### Example:
205
206
  *
206
207
  * MyModel.events.on('error', err => console.log(err.message));
207
208
  *
208
209
  * // Prints a 'CastError' because of the above handler
209
- * await MyModel.findOne({ _id: 'notanid' }).catch(noop);
210
+ * await MyModel.findOne({ _id: 'Not a valid ObjectId' }).catch(noop);
210
211
  *
211
212
  * @api public
212
213
  * @fires error whenever any query or model function errors
@@ -245,8 +246,7 @@ function _applyCustomWhere(doc, where) {
245
246
  */
246
247
 
247
248
  Model.prototype.$__handleSave = function(options, callback) {
248
- const _this = this;
249
- let saveOptions = {};
249
+ const saveOptions = {};
250
250
 
251
251
  applyWriteConcern(this.$__schema, options);
252
252
  if (typeof options.writeConcern !== 'undefined') {
@@ -274,14 +274,10 @@ Model.prototype.$__handleSave = function(options, callback) {
274
274
  if ('checkKeys' in options) {
275
275
  saveOptions.checkKeys = options.checkKeys;
276
276
  }
277
- const session = this.$session();
278
277
  if (!saveOptions.hasOwnProperty('session')) {
279
- saveOptions.session = session;
278
+ saveOptions.session = this.$session();
280
279
  }
281
280
 
282
- if (Object.keys(saveOptions).length === 0) {
283
- saveOptions = null;
284
- }
285
281
  if (this.$isNew) {
286
282
  // send entire doc
287
283
  const obj = this.toObject(saveToObjectOptions);
@@ -298,9 +294,9 @@ Model.prototype.$__handleSave = function(options, callback) {
298
294
  }
299
295
 
300
296
  this.$__version(true, obj);
301
- this[modelCollectionSymbol].insertOne(obj, saveOptions, function(err, ret) {
297
+ this[modelCollectionSymbol].insertOne(obj, saveOptions, (err, ret) => {
302
298
  if (err) {
303
- _setIsNew(_this, true);
299
+ _setIsNew(this, true);
304
300
 
305
301
  callback(err, null);
306
302
  return;
@@ -308,64 +304,68 @@ Model.prototype.$__handleSave = function(options, callback) {
308
304
 
309
305
  callback(null, ret);
310
306
  });
307
+
311
308
  this.$__reset();
312
309
  _setIsNew(this, false);
313
310
  // Make it possible to retry the insert
314
311
  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
312
 
327
- const where = this.$__where(delta[0]);
328
- if (where instanceof MongooseError) {
329
- callback(where);
330
- return;
331
- }
313
+ return;
314
+ }
332
315
 
333
- _applyCustomWhere(this, where);
334
- this[modelCollectionSymbol].updateOne(where, delta[1], saveOptions, (err, ret) => {
335
- if (err) {
336
- this.$__undoReset();
316
+ // Make sure we don't treat it as a new object on error,
317
+ // since it already exists
318
+ this.$__.inserting = false;
337
319
 
338
- callback(err);
339
- return;
340
- }
341
- ret.$where = where;
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);
320
+ const delta = this.$__delta();
321
+ if (delta) {
322
+ if (delta instanceof MongooseError) {
323
+ callback(delta);
360
324
  return;
361
325
  }
362
326
 
363
- // store the modified paths before the document is reset
364
- this.$__.modifiedPaths = this.modifiedPaths();
365
- this.$__reset();
327
+ const where = this.$__where(delta[0]);
328
+ if (where instanceof MongooseError) {
329
+ callback(where);
330
+ return;
331
+ }
366
332
 
367
- _setIsNew(this, false);
333
+ _applyCustomWhere(this, where);
334
+ this[modelCollectionSymbol].updateOne(where, delta[1], saveOptions, (err, ret) => {
335
+ if (err) {
336
+ this.$__undoReset();
337
+
338
+ callback(err);
339
+ return;
340
+ }
341
+ ret.$where = where;
342
+ callback(null, ret);
343
+ });
344
+ } else {
345
+ const optionsWithCustomValues = Object.assign({}, options, saveOptions);
346
+ const where = this.$__where();
347
+ const optimisticConcurrency = this.$__schema.options.optimisticConcurrency;
348
+ if (optimisticConcurrency) {
349
+ const key = this.$__schema.options.versionKey;
350
+ const val = this.$__getValue(key);
351
+ if (val != null) {
352
+ where[key] = val;
353
+ }
354
+ }
355
+ this.constructor.exists(where, optionsWithCustomValues)
356
+ .then(documentExists => {
357
+ const matchedCount = !documentExists ? 0 : 1;
358
+ callback(null, { $where: where, matchedCount });
359
+ })
360
+ .catch(callback);
361
+ return;
368
362
  }
363
+
364
+ // store the modified paths before the document is reset
365
+ this.$__.modifiedPaths = this.modifiedPaths();
366
+ this.$__reset();
367
+
368
+ _setIsNew(this, false);
369
369
  };
370
370
 
371
371
  /*!
@@ -374,8 +374,8 @@ Model.prototype.$__handleSave = function(options, callback) {
374
374
 
375
375
  Model.prototype.$__save = function(options, callback) {
376
376
  this.$__handleSave(options, (error, result) => {
377
- const hooks = this.$__schema.s.hooks;
378
377
  if (error) {
378
+ const hooks = this.$__schema.s.hooks;
379
379
  return hooks.execPost('save:error', this, [this], { error: error }, (error) => {
380
380
  callback(error, this);
381
381
  });
@@ -399,7 +399,7 @@ Model.prototype.$__save = function(options, callback) {
399
399
  }
400
400
  }
401
401
 
402
- const versionBump = this.$__.version || this.$__schema.options.optimisticConcurrency;
402
+ const versionBump = this.$__.version;
403
403
  // was this an update that required a version bump?
404
404
  if (versionBump && !this.$__.inserting) {
405
405
  const doIncrement = VERSION_INC === (VERSION_INC & this.$__.version);
@@ -423,6 +423,7 @@ Model.prototype.$__save = function(options, callback) {
423
423
  this.$__undoReset();
424
424
  error = new DocumentNotFoundError(result.$where,
425
425
  this.constructor.modelName, numAffected, result);
426
+ const hooks = this.$__schema.s.hooks;
426
427
  return hooks.execPost('save:error', this, [this], { error: error }, (error) => {
427
428
  callback(error, this);
428
429
  });
@@ -453,7 +454,7 @@ function generateVersionError(doc, modifiedPaths) {
453
454
  * Saves this document by inserting a new document into the database if [document.isNew](/docs/api.html#document_Document-isNew) is `true`,
454
455
  * or sends an [updateOne](/docs/api.html#document_Document-updateOne) operation with just the modified paths if `isNew` is `false`.
455
456
  *
456
- * ####Example:
457
+ * #### Example:
457
458
  *
458
459
  * product.sold = Date.now();
459
460
  * product = await product.save();
@@ -461,7 +462,7 @@ function generateVersionError(doc, modifiedPaths) {
461
462
  * If save is successful, the returned promise will fulfill with the document
462
463
  * saved.
463
464
  *
464
- * ####Example:
465
+ * #### Example:
465
466
  *
466
467
  * const newProduct = await product.save();
467
468
  * newProduct === product; // true
@@ -516,9 +517,9 @@ Model.prototype.save = function(options, fn) {
516
517
  this.$__.saveOptions = options;
517
518
 
518
519
  this.$__save(options, error => {
519
- this.$__.saving = undefined;
520
- delete this.$__.saveOptions;
521
- delete this.$__.$versionError;
520
+ this.$__.saving = null;
521
+ this.$__.saveOptions = null;
522
+ this.$__.$versionError = null;
522
523
  this.$op = null;
523
524
 
524
525
  if (error) {
@@ -576,7 +577,6 @@ function operand(self, where, delta, data, val, op) {
576
577
  if (VERSION_ALL === (VERSION_ALL & self.$__.version)) return;
577
578
 
578
579
  if (self.$__schema.options.optimisticConcurrency) {
579
- self.$__.version = VERSION_ALL;
580
580
  return;
581
581
  }
582
582
 
@@ -699,6 +699,12 @@ function handleAtomics(self, where, delta, data, value) {
699
699
 
700
700
  Model.prototype.$__delta = function() {
701
701
  const dirty = this.$__dirty();
702
+
703
+ const optimisticConcurrency = this.$__schema.options.optimisticConcurrency;
704
+ if (optimisticConcurrency) {
705
+ this.$__.version = dirty.length ? VERSION_ALL : VERSION_WHERE;
706
+ }
707
+
702
708
  if (!dirty.length && VERSION_ALL !== this.$__.version) {
703
709
  return;
704
710
  }
@@ -884,7 +890,7 @@ Model.prototype.$__version = function(where, delta) {
884
890
  /**
885
891
  * Signal that we desire an increment of this documents version.
886
892
  *
887
- * ####Example:
893
+ * #### Example:
888
894
  *
889
895
  * Model.findById(id, function (err, doc) {
890
896
  * doc.increment();
@@ -928,7 +934,7 @@ Model.prototype.$__where = function _where(where) {
928
934
  /**
929
935
  * Removes this document from the db.
930
936
  *
931
- * ####Example:
937
+ * #### Example:
932
938
  * product.remove(function (err, product) {
933
939
  * if (err) return handleError(err);
934
940
  * Product.findById(product._id, function (err, product) {
@@ -939,7 +945,7 @@ Model.prototype.$__where = function _where(where) {
939
945
  *
940
946
  * 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
947
  *
942
- * ####Example:
948
+ * #### Example:
943
949
  * product.remove().then(function (product) {
944
950
  * ...
945
951
  * }).catch(function (err) {
@@ -986,7 +992,7 @@ Model.prototype.delete = Model.prototype.remove;
986
992
  /**
987
993
  * Removes this document from the db. Equivalent to `.remove()`.
988
994
  *
989
- * ####Example:
995
+ * #### Example:
990
996
  * product = await product.deleteOne();
991
997
  * await Product.findById(product._id); // null
992
998
  *
@@ -1055,7 +1061,7 @@ Model.prototype.$__deleteOne = Model.prototype.$__remove;
1055
1061
  /**
1056
1062
  * Returns another Model instance.
1057
1063
  *
1058
- * ####Example:
1064
+ * #### Example:
1059
1065
  *
1060
1066
  * const doc = new Tank;
1061
1067
  * doc.model('User').findById(id, callback);
@@ -1075,7 +1081,7 @@ Model.prototype.model = function model(name) {
1075
1081
  * Under the hood, `MyModel.exists({ answer: 42 })` is equivalent to
1076
1082
  * `MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean()`
1077
1083
  *
1078
- * ####Example:
1084
+ * #### Example:
1079
1085
  * await Character.deleteMany({});
1080
1086
  * await Character.create({ name: 'Jean-Luc Picard' });
1081
1087
  *
@@ -1114,7 +1120,7 @@ Model.exists = function exists(filter, options, callback) {
1114
1120
  /**
1115
1121
  * Adds a discriminator type.
1116
1122
  *
1117
- * ####Example:
1123
+ * #### Example:
1118
1124
  *
1119
1125
  * function BaseSchema() {
1120
1126
  * Schema.apply(this, arguments);
@@ -1243,7 +1249,7 @@ for (const i in EventEmitter.prototype) {
1243
1249
  * to get back a promise that will resolve when your indexes are finished
1244
1250
  * building as an alternative to [`MyModel.on('index')`](/docs/guide.html#indexes)
1245
1251
  *
1246
- * ####Example:
1252
+ * #### Example:
1247
1253
  *
1248
1254
  * const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
1249
1255
  * // This calls `Event.init()` implicitly, so you don't need to call
@@ -1329,7 +1335,7 @@ Model.init = function init(callback) {
1329
1335
  * Note 2: You don't have to call this if your schema contains index or unique field.
1330
1336
  * In that case, just use `Model.init()`
1331
1337
  *
1332
- * ####Example:
1338
+ * #### Example:
1333
1339
  *
1334
1340
  * const userSchema = new Schema({ name: String })
1335
1341
  * const User = mongoose.model('User', userSchema);
@@ -1411,7 +1417,7 @@ Model.createCollection = function createCollection(options, callback) {
1411
1417
  * See the [introductory blog post](https://thecodebarbarian.com/whats-new-in-mongoose-5-2-syncindexes)
1412
1418
  * for more information.
1413
1419
  *
1414
- * ####Example:
1420
+ * #### Example:
1415
1421
  *
1416
1422
  * const schema = new Schema({ name: { type: String, unique: true } });
1417
1423
  * const Customer = mongoose.model('Customer', schema);
@@ -1557,6 +1563,7 @@ Model.cleanIndexes = function cleanIndexes(callback) {
1557
1563
 
1558
1564
  for (const [schemaIndexKeysObject, schemaIndexOptions] of schemaIndexes) {
1559
1565
  const options = decorateDiscriminatorIndexOptions(this.schema, utils.clone(schemaIndexOptions));
1566
+ applySchemaCollation(schemaIndexKeysObject, options, this.schema.options);
1560
1567
 
1561
1568
  if (isIndexEqual(schemaIndexKeysObject, options, dbIndex)) {
1562
1569
  found = true;
@@ -1629,7 +1636,7 @@ Model.listIndexes = function init(callback) {
1629
1636
  * Sends `createIndex` commands to mongo for each index declared in the schema.
1630
1637
  * The `createIndex` commands are sent in series.
1631
1638
  *
1632
- * ####Example:
1639
+ * #### Example:
1633
1640
  *
1634
1641
  * Event.ensureIndexes(function (err) {
1635
1642
  * if (err) return handleError(err);
@@ -1637,7 +1644,7 @@ Model.listIndexes = function init(callback) {
1637
1644
  *
1638
1645
  * After completion, an `index` event is emitted on this `Model` passing an error if one occurred.
1639
1646
  *
1640
- * ####Example:
1647
+ * #### Example:
1641
1648
  *
1642
1649
  * const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
1643
1650
  * const Event = mongoose.model('Event', eventSchema);
@@ -1775,26 +1782,17 @@ function _ensureIndexes(model, options, callback) {
1775
1782
 
1776
1783
  const indexFields = utils.clone(index[0]);
1777
1784
  const indexOptions = utils.clone(index[1]);
1778
- let isTextIndex = false;
1779
- for (const key of Object.keys(indexFields)) {
1780
- if (indexFields[key] === 'text') {
1781
- isTextIndex = true;
1782
- }
1783
- }
1785
+
1784
1786
  delete indexOptions._autoIndex;
1785
1787
  decorateDiscriminatorIndexOptions(model.schema, indexOptions);
1786
1788
  applyWriteConcern(model.schema, indexOptions);
1789
+ applySchemaCollation(indexFields, indexOptions, model.schema.options);
1787
1790
 
1788
1791
  indexSingleStart(indexFields, options);
1789
1792
 
1790
1793
  if ('background' in options) {
1791
1794
  indexOptions.background = options.background;
1792
1795
  }
1793
- if (model.schema.options.hasOwnProperty('collation') &&
1794
- !indexOptions.hasOwnProperty('collation') &&
1795
- !isTextIndex) {
1796
- indexOptions.collation = model.schema.options.collation;
1797
- }
1798
1796
 
1799
1797
  model.collection.createIndex(indexFields, indexOptions, utils.tick(function(err, name) {
1800
1798
  indexSingleDone(err, indexFields, indexOptions, name);
@@ -1874,7 +1872,7 @@ Model.discriminators;
1874
1872
  /**
1875
1873
  * Translate any aliases fields/conditions so the final query or document object is pure
1876
1874
  *
1877
- * ####Example:
1875
+ * #### Example:
1878
1876
  *
1879
1877
  * Character
1880
1878
  * .find(Character.translateAliases({
@@ -1882,7 +1880,7 @@ Model.discriminators;
1882
1880
  * })
1883
1881
  * .exec(function(err, characters) {})
1884
1882
  *
1885
- * ####Note:
1883
+ * #### Note:
1886
1884
  * Only translate arguments of object type anything else is returned raw
1887
1885
  *
1888
1886
  * @param {Object} fields fields/conditions that may contain aliased keys
@@ -1971,12 +1969,12 @@ Model.translateAliases = function translateAliases(fields) {
1971
1969
  *
1972
1970
  * This method is deprecated. See [Deprecation Warnings](../deprecations.html#remove) for details.
1973
1971
  *
1974
- * ####Example:
1972
+ * #### Example:
1975
1973
  *
1976
1974
  * const res = await Character.remove({ name: 'Eddard Stark' });
1977
1975
  * res.deletedCount; // Number of documents removed
1978
1976
  *
1979
- * ####Note:
1977
+ * #### Note:
1980
1978
  *
1981
1979
  * This method sends a remove command directly to MongoDB, no Mongoose documents
1982
1980
  * are involved. Because no Mongoose documents are involved, Mongoose does
@@ -2018,11 +2016,11 @@ Model.remove = function remove(conditions, options, callback) {
2018
2016
  * Behaves like `remove()`, but deletes at most one document regardless of the
2019
2017
  * `single` option.
2020
2018
  *
2021
- * ####Example:
2019
+ * #### Example:
2022
2020
  *
2023
2021
  * await Character.deleteOne({ name: 'Eddard Stark' }); // returns {deletedCount: 1}
2024
2022
  *
2025
- * ####Note:
2023
+ * #### Note:
2026
2024
  *
2027
2025
  * This function triggers `deleteOne` query hooks. Read the
2028
2026
  * [middleware docs](/docs/middleware.html#naming) to learn more.
@@ -2061,11 +2059,11 @@ Model.deleteOne = function deleteOne(conditions, options, callback) {
2061
2059
  * Behaves like `remove()`, but deletes all documents that match `conditions`
2062
2060
  * regardless of the `single` option.
2063
2061
  *
2064
- * ####Example:
2062
+ * #### Example:
2065
2063
  *
2066
2064
  * await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }); // returns {deletedCount: x} where x is the number of documents deleted.
2067
2065
  *
2068
- * ####Note:
2066
+ * #### Note:
2069
2067
  *
2070
2068
  * This function triggers `deleteMany` query hooks. Read the
2071
2069
  * [middleware docs](/docs/middleware.html#naming) to learn more.
@@ -2104,7 +2102,7 @@ Model.deleteMany = function deleteMany(conditions, options, callback) {
2104
2102
  * See our [query casting tutorial](/docs/tutorials/query_casting.html) for
2105
2103
  * more information on how Mongoose casts `filter`.
2106
2104
  *
2107
- * ####Examples:
2105
+ * #### Examples:
2108
2106
  *
2109
2107
  * // find all documents
2110
2108
  * await MyModel.find({});
@@ -2180,7 +2178,7 @@ Model.find = function find(conditions, projection, options, callback) {
2180
2178
  * to `findOne({})` and return arbitrary documents. However, mongoose
2181
2179
  * translates `findById(undefined)` into `findOne({ _id: null })`.
2182
2180
  *
2183
- * ####Example:
2181
+ * #### Example:
2184
2182
  *
2185
2183
  * // Find the adventure with the given `id`, or `null` if not found
2186
2184
  * await Adventure.findById(id).exec();
@@ -2223,7 +2221,7 @@ Model.findById = function findById(id, projection, options, callback) {
2223
2221
  * mongoose will send an empty `findOne` command to MongoDB, which will return
2224
2222
  * an arbitrary document. If you're querying by `_id`, use `findById()` instead.
2225
2223
  *
2226
- * ####Example:
2224
+ * #### Example:
2227
2225
  *
2228
2226
  * // Find one adventure whose `country` is 'Croatia', otherwise `null`
2229
2227
  * await Adventure.findOne({ country: 'Croatia' }).exec();
@@ -2279,7 +2277,7 @@ Model.findOne = function findOne(conditions, projection, options, callback) {
2279
2277
  * `estimatedDocumentCount()` uses collection metadata rather than scanning
2280
2278
  * the entire collection.
2281
2279
  *
2282
- * ####Example:
2280
+ * #### Example:
2283
2281
  *
2284
2282
  * const numAdventures = await Adventure.estimatedDocumentCount();
2285
2283
  *
@@ -2302,7 +2300,7 @@ Model.estimatedDocumentCount = function estimatedDocumentCount(options, callback
2302
2300
  /**
2303
2301
  * Counts number of documents matching `filter` in a database collection.
2304
2302
  *
2305
- * ####Example:
2303
+ * #### Example:
2306
2304
  *
2307
2305
  * Adventure.countDocuments({ type: 'jungle' }, function (err, count) {
2308
2306
  * console.log('there are %d jungle adventures', count);
@@ -2357,7 +2355,7 @@ Model.countDocuments = function countDocuments(conditions, options, callback) {
2357
2355
  * a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model.estimatedDocumentCount)
2358
2356
  * instead. Otherwise, use the [`countDocuments()`](/docs/api.html#model_Model.countDocuments) function instead.
2359
2357
  *
2360
- * ####Example:
2358
+ * #### Example:
2361
2359
  *
2362
2360
  * const count = await Adventure.count({ type: 'jungle' });
2363
2361
  * console.log('there are %d jungle adventures', count);
@@ -2389,7 +2387,7 @@ Model.count = function count(conditions, callback) {
2389
2387
  *
2390
2388
  * Passing a `callback` executes the query.
2391
2389
  *
2392
- * ####Example
2390
+ * #### Example
2393
2391
  *
2394
2392
  * Link.distinct('url', { clicks: {$gt: 100}}, function (err, result) {
2395
2393
  * if (err) return handleError(err);
@@ -2481,7 +2479,7 @@ Model.$where = function $where() {
2481
2479
  *
2482
2480
  * 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
2481
  *
2484
- * ####Options:
2482
+ * #### Options:
2485
2483
  *
2486
2484
  * - `new`: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
2487
2485
  * - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
@@ -2494,7 +2492,7 @@ Model.$where = function $where() {
2494
2492
  * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2495
2493
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2496
2494
  *
2497
- * ####Examples:
2495
+ * #### Examples:
2498
2496
  *
2499
2497
  * A.findOneAndUpdate(conditions, update, options, callback) // executes
2500
2498
  * A.findOneAndUpdate(conditions, update, options) // returns Query
@@ -2502,11 +2500,11 @@ Model.$where = function $where() {
2502
2500
  * A.findOneAndUpdate(conditions, update) // returns Query
2503
2501
  * A.findOneAndUpdate() // returns Query
2504
2502
  *
2505
- * ####Note:
2503
+ * #### Note:
2506
2504
  *
2507
2505
  * All top level update keys which are not `atomic` operation names are treated as set operations:
2508
2506
  *
2509
- * ####Example:
2507
+ * #### Example:
2510
2508
  *
2511
2509
  * const query = { name: 'borne' };
2512
2510
  * Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)
@@ -2516,7 +2514,7 @@ Model.$where = function $where() {
2516
2514
  *
2517
2515
  * This helps prevent accidentally overwriting your document with `{ name: 'jason bourne' }`.
2518
2516
  *
2519
- * ####Note:
2517
+ * #### Note:
2520
2518
  *
2521
2519
  * `findOneAndX` and `findByIdAndX` functions support limited validation that
2522
2520
  * you can enable by setting the `runValidators` option.
@@ -2619,7 +2617,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
2619
2617
  *
2620
2618
  * - `findOneAndUpdate()`
2621
2619
  *
2622
- * ####Options:
2620
+ * #### Options:
2623
2621
  *
2624
2622
  * - `new`: bool - true to return the modified document rather than the original. defaults to false
2625
2623
  * - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
@@ -2630,7 +2628,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
2630
2628
  * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2631
2629
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2632
2630
  *
2633
- * ####Examples:
2631
+ * #### Examples:
2634
2632
  *
2635
2633
  * A.findByIdAndUpdate(id, update, options, callback) // executes
2636
2634
  * A.findByIdAndUpdate(id, update, options) // returns Query
@@ -2638,11 +2636,11 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
2638
2636
  * A.findByIdAndUpdate(id, update) // returns Query
2639
2637
  * A.findByIdAndUpdate() // returns Query
2640
2638
  *
2641
- * ####Note:
2639
+ * #### Note:
2642
2640
  *
2643
2641
  * All top level update keys which are not `atomic` operation names are treated as set operations:
2644
2642
  *
2645
- * ####Example:
2643
+ * #### Example:
2646
2644
  *
2647
2645
  * Model.findByIdAndUpdate(id, { name: 'jason bourne' }, options, callback)
2648
2646
  *
@@ -2651,7 +2649,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
2651
2649
  *
2652
2650
  * This helps prevent accidentally overwriting your document with `{ name: 'jason bourne' }`.
2653
2651
  *
2654
- * ####Note:
2652
+ * #### Note:
2655
2653
  *
2656
2654
  * `findOneAndX` and `findByIdAndX` functions support limited validation. You can
2657
2655
  * enable validation by setting the `runValidators` option.
@@ -2722,7 +2720,7 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
2722
2720
  * this distinction is purely pedantic. You should use `findOneAndDelete()`
2723
2721
  * unless you have a good reason not to.
2724
2722
  *
2725
- * ####Options:
2723
+ * #### Options:
2726
2724
  *
2727
2725
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
2728
2726
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
@@ -2731,7 +2729,7 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
2731
2729
  * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2732
2730
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2733
2731
  *
2734
- * ####Examples:
2732
+ * #### Examples:
2735
2733
  *
2736
2734
  * A.findOneAndDelete(conditions, options, callback) // executes
2737
2735
  * A.findOneAndDelete(conditions, options) // return Query
@@ -2835,7 +2833,7 @@ Model.findByIdAndDelete = function(id, options, callback) {
2835
2833
  *
2836
2834
  * - `findOneAndReplace()`
2837
2835
  *
2838
- * ####Options:
2836
+ * #### Options:
2839
2837
  *
2840
2838
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
2841
2839
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
@@ -2844,7 +2842,7 @@ Model.findByIdAndDelete = function(id, options, callback) {
2844
2842
  * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2845
2843
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2846
2844
  *
2847
- * ####Examples:
2845
+ * #### Examples:
2848
2846
  *
2849
2847
  * A.findOneAndReplace(filter, replacement, options, callback) // executes
2850
2848
  * A.findOneAndReplace(filter, replacement, options) // return Query
@@ -2914,7 +2912,7 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
2914
2912
  *
2915
2913
  * - `findOneAndRemove()`
2916
2914
  *
2917
- * ####Options:
2915
+ * #### Options:
2918
2916
  *
2919
2917
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
2920
2918
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
@@ -2923,7 +2921,7 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
2923
2921
  * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2924
2922
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2925
2923
  *
2926
- * ####Examples:
2924
+ * #### Examples:
2927
2925
  *
2928
2926
  * A.findOneAndRemove(conditions, options, callback) // executes
2929
2927
  * A.findOneAndRemove(conditions, options) // return Query
@@ -2994,14 +2992,14 @@ Model.findOneAndRemove = function(conditions, options, callback) {
2994
2992
  *
2995
2993
  * - `findOneAndRemove()`
2996
2994
  *
2997
- * ####Options:
2995
+ * #### Options:
2998
2996
  *
2999
2997
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
3000
2998
  * - `select`: sets the document fields to return
3001
2999
  * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3002
3000
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
3003
3001
  *
3004
- * ####Examples:
3002
+ * #### Examples:
3005
3003
  *
3006
3004
  * A.findByIdAndRemove(id, options, callback) // executes
3007
3005
  * A.findByIdAndRemove(id, options) // return Query
@@ -3044,7 +3042,7 @@ Model.findByIdAndRemove = function(id, options, callback) {
3044
3042
  *
3045
3043
  * - `save()`
3046
3044
  *
3047
- * ####Example:
3045
+ * #### Example:
3048
3046
  *
3049
3047
  * // Insert one new `Character` document
3050
3048
  * await Character.create({ name: 'Jean-Luc Picard' });
@@ -3196,7 +3194,7 @@ Model.create = function create(doc, options, callback) {
3196
3194
  * - 'end': Emitted if the underlying stream is closed
3197
3195
  * - 'close': Emitted if the underlying stream is closed
3198
3196
  *
3199
- * ####Example:
3197
+ * #### Example:
3200
3198
  *
3201
3199
  * const doc = await Person.create({ name: 'Ned Stark' });
3202
3200
  * const changeStream = Person.watch().on('change', change => console.log(change));
@@ -3245,7 +3243,7 @@ Model.watch = function(pipeline, options) {
3245
3243
  *
3246
3244
  * This function does not trigger any middleware.
3247
3245
  *
3248
- * ####Example:
3246
+ * #### Example:
3249
3247
  *
3250
3248
  * const session = await Person.startSession();
3251
3249
  * let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
@@ -3286,7 +3284,7 @@ Model.startSession = function() {
3286
3284
  *
3287
3285
  * - `insertMany()`
3288
3286
  *
3289
- * ####Example:
3287
+ * #### Example:
3290
3288
  *
3291
3289
  * const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
3292
3290
  * Movies.insertMany(arr, function(error, docs) {});
@@ -3506,7 +3504,7 @@ function _setIsNew(doc, val) {
3506
3504
  * If you need to trigger
3507
3505
  * `save()` middleware for every document use [`create()`](https://mongoosejs.com/docs/api.html#model_Model.create) instead.
3508
3506
  *
3509
- * ####Example:
3507
+ * #### Example:
3510
3508
  *
3511
3509
  * Character.bulkWrite([
3512
3510
  * {
@@ -3762,7 +3760,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
3762
3760
  * Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
3763
3761
  * The document returned has no paths marked as modified initially.
3764
3762
  *
3765
- * ####Example:
3763
+ * #### Example:
3766
3764
  *
3767
3765
  * // hydrate previous data into a Mongoose document
3768
3766
  * const mongooseCandy = Candy.hydrate({ _id: '54108337212ffb6d459f854c', type: 'jelly bean' });
@@ -3797,7 +3795,7 @@ Model.hydrate = function(obj, projection) {
3797
3795
  *
3798
3796
  * This method is deprecated. See [Deprecation Warnings](../deprecations.html#update) for details.
3799
3797
  *
3800
- * ####Examples:
3798
+ * #### Examples:
3801
3799
  *
3802
3800
  * MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
3803
3801
  *
@@ -3807,7 +3805,7 @@ Model.hydrate = function(obj, projection) {
3807
3805
  * // had `ferret` set to `true`, `nModified` will be 0.
3808
3806
  * res.nModified;
3809
3807
  *
3810
- * ####Valid options:
3808
+ * #### Valid options:
3811
3809
  *
3812
3810
  * - `strict` (boolean): overrides the [schema-level `strict` option](/docs/guide.html#strict) for this update
3813
3811
  * - `upsert` (boolean): whether to create the doc if it doesn't match (false)
@@ -3825,11 +3823,11 @@ Model.hydrate = function(obj, projection) {
3825
3823
  * - `err` is the error if any occurred
3826
3824
  * - `rawResponse` is the full response from Mongo
3827
3825
  *
3828
- * ####Note:
3826
+ * #### Note:
3829
3827
  *
3830
3828
  * All top level keys which are not `atomic` operation names are treated as set operations:
3831
3829
  *
3832
- * ####Example:
3830
+ * #### Example:
3833
3831
  *
3834
3832
  * const query = { name: 'borne' };
3835
3833
  * Model.update(query, { name: 'jason bourne' }, options, callback);
@@ -3840,7 +3838,7 @@ Model.hydrate = function(obj, projection) {
3840
3838
  *
3841
3839
  * This helps prevent accidentally overwriting all documents in your collection with `{ name: 'jason bourne' }`.
3842
3840
  *
3843
- * ####Note:
3841
+ * #### Note:
3844
3842
  *
3845
3843
  * 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
3844
  *
@@ -3881,7 +3879,7 @@ Model.update = function update(conditions, doc, options, callback) {
3881
3879
  * **Note** updateMany will _not_ fire update middleware. Use `pre('updateMany')`
3882
3880
  * and `post('updateMany')` instead.
3883
3881
  *
3884
- * ####Example:
3882
+ * #### Example:
3885
3883
  * const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
3886
3884
  * res.matchedCount; // Number of documents matched
3887
3885
  * res.modifiedCount; // Number of documents modified
@@ -3921,7 +3919,7 @@ Model.updateMany = function updateMany(conditions, doc, options, callback) {
3921
3919
  * - MongoDB will update _only_ the first document that matches `filter` regardless of the value of the `multi` option.
3922
3920
  * - Use `replaceOne()` if you want to overwrite an entire document rather than using atomic operators like `$set`.
3923
3921
  *
3924
- * ####Example:
3922
+ * #### Example:
3925
3923
  * const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
3926
3924
  * res.matchedCount; // Number of documents matched
3927
3925
  * res.modifiedCount; // Number of documents modified
@@ -3958,7 +3956,7 @@ Model.updateOne = function updateOne(conditions, doc, options, callback) {
3958
3956
  * Same as `update()`, except MongoDB replace the existing document with the
3959
3957
  * given document (no atomic operators like `$set`).
3960
3958
  *
3961
- * ####Example:
3959
+ * #### Example:
3962
3960
  * const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
3963
3961
  * res.matchedCount; // Number of documents matched
3964
3962
  * res.modifiedCount; // Number of documents modified
@@ -4029,7 +4027,7 @@ function _update(model, op, conditions, doc, options, callback) {
4029
4027
  *
4030
4028
  * This function does not trigger any middleware.
4031
4029
  *
4032
- * ####Example:
4030
+ * #### Example:
4033
4031
  *
4034
4032
  * const o = {};
4035
4033
  * // `map()` and `reduce()` are run on the MongoDB server, not Node.js,
@@ -4040,7 +4038,7 @@ function _update(model, op, conditions, doc, options, callback) {
4040
4038
  * console.log(results)
4041
4039
  * })
4042
4040
  *
4043
- * ####Other options:
4041
+ * #### Other options:
4044
4042
  *
4045
4043
  * - `query` {Object} query filter object.
4046
4044
  * - `sort` {Object} sort input objects using this key
@@ -4053,7 +4051,7 @@ function _update(model, op, conditions, doc, options, callback) {
4053
4051
  * - `readPreference` {String}
4054
4052
  * - `out*` {Object, default: {inline:1}} sets the output target for the map reduce job.
4055
4053
  *
4056
- * ####* out options:
4054
+ * #### * out options:
4057
4055
  *
4058
4056
  * - `{inline:1}` the results are returned in an array
4059
4057
  * - `{replace: 'collectionName'}` add the results to collectionName: the results replace the collection
@@ -4062,7 +4060,7 @@ function _update(model, op, conditions, doc, options, callback) {
4062
4060
  *
4063
4061
  * 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
4062
  *
4065
- * ####Example:
4063
+ * #### Example:
4066
4064
  *
4067
4065
  * const o = {};
4068
4066
  * // You can also define `map()` and `reduce()` as strings if your
@@ -4155,7 +4153,7 @@ Model.mapReduce = function mapReduce(o, callback) {
4155
4153
  *
4156
4154
  * - `aggregate()`
4157
4155
  *
4158
- * ####Example:
4156
+ * #### Example:
4159
4157
  *
4160
4158
  * // Find the max balance of all accounts
4161
4159
  * const res = await Users.aggregate([
@@ -4172,7 +4170,7 @@ Model.mapReduce = function mapReduce(o, callback) {
4172
4170
  * exec();
4173
4171
  * console.log(res); // [ { maxBalance: 98 } ]
4174
4172
  *
4175
- * ####NOTE:
4173
+ * #### Note:
4176
4174
  *
4177
4175
  * - 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
4176
  * - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
@@ -4234,7 +4232,7 @@ Model.aggregate = function aggregate(pipeline, options, callback) {
4234
4232
  * Casts and validates the given object against this model's schema, passing the
4235
4233
  * given `context` to custom validators.
4236
4234
  *
4237
- * ####Example:
4235
+ * #### Example:
4238
4236
  *
4239
4237
  * const Model = mongoose.model('Test', Schema({
4240
4238
  * name: { type: String, required: true },
@@ -4368,7 +4366,7 @@ Model.validate = function validate(obj, pathsToValidate, context, callback) {
4368
4366
  * Changed in Mongoose 6: the model you call `populate()` on should be the
4369
4367
  * "local field" model, **not** the "foreign field" model.
4370
4368
  *
4371
- * ####Available top-level options:
4369
+ * #### Available top-level options:
4372
4370
  *
4373
4371
  * - path: space delimited path(s) to populate
4374
4372
  * - select: optional fields to select
@@ -4378,7 +4376,7 @@ Model.validate = function validate(obj, pathsToValidate, context, callback) {
4378
4376
  * - justOne: optional boolean, if true Mongoose will always set `path` to an array. Inferred from schema by default.
4379
4377
  * - strictPopulate: optional boolean, set to `false` to allow populating paths that aren't in the schema.
4380
4378
  *
4381
- * ####Examples:
4379
+ * #### Examples:
4382
4380
  *
4383
4381
  * const Dog = mongoose.model('Dog', new Schema({ name: String, breed: String }));
4384
4382
  * const Person = mongoose.model('Person', new Schema({
@@ -5028,7 +5026,7 @@ Model.$wrapCallback = function(callback) {
5028
5026
  * Helper for console.log. Given a model named 'MyModel', returns the string
5029
5027
  * `'Model { MyModel }'`.
5030
5028
  *
5031
- * ####Example:
5029
+ * #### Example:
5032
5030
  *
5033
5031
  * const MyModel = mongoose.model('Test', Schema({ name: String }));
5034
5032
  * MyModel.inspect(); // 'Model { Test }'