mongoose 7.1.1 → 7.2.0

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/lib/query.js CHANGED
@@ -154,6 +154,18 @@ Query.prototype = new mquery();
154
154
  Query.prototype.constructor = Query;
155
155
  Query.base = mquery.prototype;
156
156
 
157
+ /*!
158
+ * Overwrite mquery's `_distinct`, because Mongoose uses that name
159
+ * to store the field to apply distinct on.
160
+ */
161
+
162
+ Object.defineProperty(Query.prototype, '_distinct', {
163
+ configurable: true,
164
+ writable: true,
165
+ enumerable: true,
166
+ value: undefined
167
+ });
168
+
157
169
  /**
158
170
  * Flag to opt out of using `$geoWithin`.
159
171
  *
@@ -1618,6 +1630,11 @@ Query.prototype.setOptions = function(options, overwrite) {
1618
1630
  this._mongooseOptions.defaults = options.defaults;
1619
1631
  // deleting options.defaults will cause 7287 to fail
1620
1632
  }
1633
+ if ('translateAliases' in options) {
1634
+ this._mongooseOptions.translateAliases = options.translateAliases;
1635
+ delete options.translateAliases;
1636
+ }
1637
+
1621
1638
  if (options.lean == null && this.schema && 'lean' in this.schema.options) {
1622
1639
  this._mongooseOptions.lean = this.schema.options.lean;
1623
1640
  }
@@ -2237,6 +2254,9 @@ Query.prototype._find = async function _find() {
2237
2254
  });
2238
2255
 
2239
2256
  const options = this._optionsForExec();
2257
+
2258
+ this._applyTranslateAliases(options);
2259
+
2240
2260
  const filter = this._conditions;
2241
2261
  const fields = options.projection;
2242
2262
 
@@ -2486,6 +2506,8 @@ Query.prototype._findOne = async function _findOne() {
2486
2506
 
2487
2507
  const options = this._optionsForExec();
2488
2508
 
2509
+ this._applyTranslateAliases(options);
2510
+
2489
2511
  // don't pass in the conditions because we already merged them in
2490
2512
  const doc = await this._collection.collection.findOne(this._conditions, options);
2491
2513
  return new Promise((resolve, reject) => {
@@ -2520,6 +2542,7 @@ Query.prototype._findOne = async function _findOne() {
2520
2542
  * @param {Object} [filter] mongodb selector
2521
2543
  * @param {Object} [projection] optional fields to return
2522
2544
  * @param {Object} [options] see [`setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
2545
+ * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
2523
2546
  * @return {Query} this
2524
2547
  * @see findOne https://www.mongodb.com/docs/manual/reference/method/db.collection.findOne/
2525
2548
  * @see Query.select https://mongoosejs.com/docs/api/query.html#Query.prototype.select()
@@ -2582,9 +2605,12 @@ Query.prototype._count = async function _count() {
2582
2605
  applyGlobalMaxTimeMS(this.options, this.model);
2583
2606
  applyGlobalDiskUse(this.options, this.model);
2584
2607
 
2585
- const conds = this._conditions;
2586
2608
  const options = this._optionsForExec();
2587
2609
 
2610
+ this._applyTranslateAliases(options);
2611
+
2612
+ const conds = this._conditions;
2613
+
2588
2614
  return this._collection.collection.count(conds, options);
2589
2615
  };
2590
2616
 
@@ -2609,12 +2635,43 @@ Query.prototype._countDocuments = async function _countDocuments() {
2609
2635
  applyGlobalMaxTimeMS(this.options, this.model);
2610
2636
  applyGlobalDiskUse(this.options, this.model);
2611
2637
 
2612
- const conds = this._conditions;
2613
2638
  const options = this._optionsForExec();
2614
2639
 
2640
+ this._applyTranslateAliases(options);
2641
+
2642
+ const conds = this._conditions;
2643
+
2615
2644
  return this._collection.collection.countDocuments(conds, options);
2616
2645
  };
2617
2646
 
2647
+ /*!
2648
+ * If `translateAliases` option is set, call `Model.translateAliases()`
2649
+ * on the following query properties: filter, projection, update, distinct.
2650
+ */
2651
+
2652
+ Query.prototype._applyTranslateAliases = function _applyTranslateAliases(options) {
2653
+ let applyTranslateAliases = false;
2654
+ if ('translateAliases' in this._mongooseOptions) {
2655
+ applyTranslateAliases = this._mongooseOptions.translateAliases;
2656
+ } else if (this.model?.schema?._userProvidedOptions?.translateAliases != null) {
2657
+ applyTranslateAliases = this.model.schema._userProvidedOptions.translateAliases;
2658
+ } else if (this.model?.base?.options?.translateAliases != null) {
2659
+ applyTranslateAliases = this.model.base.options.translateAliases;
2660
+ }
2661
+ if (!applyTranslateAliases) {
2662
+ return;
2663
+ }
2664
+
2665
+ if (this.model?.schema?.aliases && Object.keys(this.model.schema.aliases).length > 0) {
2666
+ this.model.translateAliases(this._conditions, true);
2667
+ this.model.translateAliases(options.projection, true);
2668
+ this.model.translateAliases(this._update, true);
2669
+ if (this._distinct != null && this.model.schema.aliases[this._distinct] != null) {
2670
+ this._distinct = this.model.schema.aliases[this._distinct];
2671
+ }
2672
+ }
2673
+ };
2674
+
2618
2675
  /**
2619
2676
  * Execute a estimatedDocumentCount() query
2620
2677
  *
@@ -2796,6 +2853,7 @@ Query.prototype.__distinct = async function __distinct() {
2796
2853
  applyGlobalDiskUse(this.options, this.model);
2797
2854
 
2798
2855
  const options = this._optionsForExec();
2856
+ this._applyTranslateAliases(options);
2799
2857
 
2800
2858
  return this._collection.collection.
2801
2859
  distinct(this._distinct, this._conditions, options);
@@ -2955,6 +3013,7 @@ Query.prototype._deleteOne = async function _deleteOne() {
2955
3013
  }
2956
3014
 
2957
3015
  const options = this._optionsForExec();
3016
+ this._applyTranslateAliases(options);
2958
3017
 
2959
3018
  return this._collection.collection.deleteOne(this._conditions, options);
2960
3019
  };
@@ -3032,6 +3091,7 @@ Query.prototype._deleteMany = async function _deleteMany() {
3032
3091
  }
3033
3092
 
3034
3093
  const options = this._optionsForExec();
3094
+ this._applyTranslateAliases(options);
3035
3095
 
3036
3096
  return this._collection.collection.deleteMany(this._conditions, options);
3037
3097
  };
@@ -3143,6 +3203,7 @@ function prepareDiscriminatorCriteria(query) {
3143
3203
  * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
3144
3204
  * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
3145
3205
  * @param {Boolean} [options.returnOriginal=null] An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`.
3206
+ * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
3146
3207
  * @see Tutorial https://mongoosejs.com/docs/tutorials/findoneandupdate.html
3147
3208
  * @see findAndModify command https://www.mongodb.com/docs/manual/reference/command/findAndModify/
3148
3209
  * @see ModifyResult https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ModifyResult.html
@@ -3234,6 +3295,7 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
3234
3295
  applyGlobalDiskUse(this.options, this.model);
3235
3296
 
3236
3297
  const opts = this._optionsForExec(this.model);
3298
+ this._applyTranslateAliases(opts);
3237
3299
 
3238
3300
  if ('strict' in opts) {
3239
3301
  this._mongooseOptions.strict = opts.strict;
@@ -3427,6 +3489,7 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
3427
3489
 
3428
3490
  const filter = this._conditions;
3429
3491
  const options = this._optionsForExec();
3492
+ this._applyTranslateAliases(options);
3430
3493
  let fields = null;
3431
3494
 
3432
3495
  this._applyPaths();
@@ -3496,6 +3559,7 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
3496
3559
  * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
3497
3560
  * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
3498
3561
  * @param {Boolean} [options.returnOriginal=null] An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`.
3562
+ * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
3499
3563
  * @return {Query} this
3500
3564
  * @api public
3501
3565
  */
@@ -3560,6 +3624,7 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {
3560
3624
 
3561
3625
  const filter = this._conditions;
3562
3626
  const options = this._optionsForExec();
3627
+ this._applyTranslateAliases(options);
3563
3628
  convertNewToReturnDocument(options);
3564
3629
 
3565
3630
  const runValidators = _getOption(this, 'runValidators', false);
@@ -3787,6 +3852,7 @@ async function _updateThunk(op) {
3787
3852
 
3788
3853
  const castedQuery = this._conditions;
3789
3854
  const options = this._optionsForExec(this.model);
3855
+ this._applyTranslateAliases(options);
3790
3856
 
3791
3857
  this._update = clone(this._update, options);
3792
3858
  const isOverwriting = this._mongooseOptions.overwrite && !hasDollarKeys(this._update);
@@ -3927,6 +3993,7 @@ Query.prototype._replaceOne = async function _replaceOne() {
3927
3993
  * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
3928
3994
  * @param {Object} [options.writeConcern=null] sets the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](https://mongoosejs.com/docs/guide.html#writeConcern)
3929
3995
  * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
3996
+ * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
3930
3997
  * @param {Function} [callback] params are (error, writeOpResult)
3931
3998
  * @return {Query} this
3932
3999
  * @see Model.update https://mongoosejs.com/docs/api/model.html#Model.update()
@@ -3995,6 +4062,7 @@ Query.prototype.updateMany = function(conditions, doc, options, callback) {
3995
4062
  * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
3996
4063
  * @param {Object} [options.writeConcern=null] sets the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](https://mongoosejs.com/docs/guide.html#writeConcern)
3997
4064
  * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
4065
+ @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
3998
4066
  * @param {Function} [callback] params are (error, writeOpResult)
3999
4067
  * @return {Query} this
4000
4068
  * @see Model.update https://mongoosejs.com/docs/api/model.html#Model.update()
@@ -4061,6 +4129,7 @@ Query.prototype.updateOne = function(conditions, doc, options, callback) {
4061
4129
  * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
4062
4130
  * @param {Object} [options.writeConcern=null] sets the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](https://mongoosejs.com/docs/guide.html#writeConcern)
4063
4131
  * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
4132
+ * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
4064
4133
  * @param {Function} [callback] params are (error, writeOpResult)
4065
4134
  * @return {Query} this
4066
4135
  * @see Model.update https://mongoosejs.com/docs/api/model.html#Model.update()
@@ -18,6 +18,7 @@ const getConstructor = require('../helpers/discriminator/getConstructor');
18
18
  const handleIdOption = require('../helpers/schema/handleIdOption');
19
19
  const internalToObjectOptions = require('../options').internalToObjectOptions;
20
20
  const utils = require('../utils');
21
+ const InvalidSchemaOptionError = require('../error/invalidSchemaOption');
21
22
 
22
23
  let Subdocument;
23
24
 
@@ -34,6 +35,9 @@ module.exports = SubdocumentPath;
34
35
  */
35
36
 
36
37
  function SubdocumentPath(schema, path, options) {
38
+ if (schema.options.timeseries) {
39
+ throw new InvalidSchemaOptionError(path, 'timeseries');
40
+ }
37
41
  const schemaTypeIdOption = SubdocumentPath.defaultOptions &&
38
42
  SubdocumentPath.defaultOptions._id;
39
43
  if (schemaTypeIdOption != null) {
@@ -84,6 +88,7 @@ function _createConstructor(schema, baseClass) {
84
88
  _embedded.prototype = Object.create(proto);
85
89
  _embedded.prototype.$__setSchema(schema);
86
90
  _embedded.prototype.constructor = _embedded;
91
+ _embedded.base = schema.base;
87
92
  _embedded.schema = schema;
88
93
  _embedded.$isSingleNested = true;
89
94
  _embedded.events = new EventEmitter();
@@ -16,6 +16,7 @@ const handleIdOption = require('../helpers/schema/handleIdOption');
16
16
  const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
17
17
  const utils = require('../utils');
18
18
  const getConstructor = require('../helpers/discriminator/getConstructor');
19
+ const InvalidSchemaOptionError = require('../error/invalidSchemaOption');
19
20
 
20
21
  const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
21
22
  const arrayPathSymbol = require('../helpers/symbols').arrayPathSymbol;
@@ -36,6 +37,9 @@ let Subdocument;
36
37
  */
37
38
 
38
39
  function DocumentArrayPath(key, schema, options, schemaOptions) {
40
+ if (schema.options.timeseries) {
41
+ throw new InvalidSchemaOptionError(key, 'timeseries');
42
+ }
39
43
  const schemaTypeIdOption = DocumentArrayPath.defaultOptions &&
40
44
  DocumentArrayPath.defaultOptions._id;
41
45
  if (schemaTypeIdOption != null) {
package/lib/schema.js CHANGED
@@ -78,6 +78,7 @@ let id = 0;
78
78
  * - [skipVersioning](https://mongoosejs.com/docs/guide.html#skipVersioning): object - paths to exclude from versioning
79
79
  * - [timestamps](https://mongoosejs.com/docs/guide.html#timestamps): object or boolean - defaults to `false`. If true, Mongoose adds `createdAt` and `updatedAt` properties to your schema and manages those properties for you.
80
80
  * - [pluginTags](https://mongoosejs.com/docs/guide.html#pluginTags): array of strings - defaults to `undefined`. If set and plugin called with `tags` option, will only apply that plugin to schemas with a matching tag.
81
+ * - [virtuals](https://mongoosejs.com/docs/tutorials/virtuals.html#virtuals-via-schema-options): object - virtuals to define, alias for [`.virtual`](https://mongoosejs.com/docs/api/schema.html#Schema.prototype.virtual())
81
82
  *
82
83
  * #### Options for Nested Schemas:
83
84
  *
@@ -743,6 +744,12 @@ Schema.prototype.add = function add(obj, prefix) {
743
744
  if (this._userProvidedOptions.strict != null) {
744
745
  childSchemaOptions.strict = this._userProvidedOptions.strict;
745
746
  }
747
+ if (this._userProvidedOptions.toObject != null) {
748
+ childSchemaOptions.toObject = this._userProvidedOptions.toObject;
749
+ }
750
+ if (this._userProvidedOptions.toJSON != null) {
751
+ childSchemaOptions.toJSON = this._userProvidedOptions.toJSON;
752
+ }
746
753
 
747
754
  const _schema = new Schema(_typeDef, childSchemaOptions);
748
755
  _schema.$implicitlyCreated = true;
@@ -1327,6 +1334,12 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
1327
1334
  if (options.hasOwnProperty('strictQuery')) {
1328
1335
  childSchemaOptions.strictQuery = options.strictQuery;
1329
1336
  }
1337
+ if (options.hasOwnProperty('toObject')) {
1338
+ childSchemaOptions.toObject = options.toObject;
1339
+ }
1340
+ if (options.hasOwnProperty('toJSON')) {
1341
+ childSchemaOptions.toJSON = options.toJSON;
1342
+ }
1330
1343
 
1331
1344
  if (this._userProvidedOptions.hasOwnProperty('_id')) {
1332
1345
  childSchemaOptions._id = this._userProvidedOptions._id;
@@ -30,7 +30,8 @@ const VALID_OPTIONS = Object.freeze([
30
30
  'strictPopulate',
31
31
  'strictQuery',
32
32
  'toJSON',
33
- 'toObject'
33
+ 'toObject',
34
+ 'translateAliases'
34
35
  ]);
35
36
 
36
37
  module.exports = VALID_OPTIONS;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "7.1.1",
4
+ "version": "7.2.0",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -19,9 +19,9 @@
19
19
  ],
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "bson": "^5.2.0",
22
+ "bson": "^5.3.0",
23
23
  "kareem": "2.5.1",
24
- "mongodb": "5.3.0",
24
+ "mongodb": "5.5.0",
25
25
  "mpath": "0.9.0",
26
26
  "mquery": "5.0.0",
27
27
  "ms": "2.1.3",
@@ -118,7 +118,7 @@
118
118
  "main": "./index.js",
119
119
  "types": "./types/index.d.ts",
120
120
  "engines": {
121
- "node": ">=14.0.0"
121
+ "node": ">=14.20.1"
122
122
  },
123
123
  "bugs": {
124
124
  "url": "https://github.com/Automattic/mongoose/issues/new"
package/types/error.d.ts CHANGED
@@ -6,7 +6,7 @@ declare module 'mongoose' {
6
6
  type CastError = Error.CastError;
7
7
  type SyncIndexesError = Error.SyncIndexesError;
8
8
 
9
- class MongooseError extends global.Error {
9
+ export class MongooseError extends global.Error {
10
10
  constructor(msg: string);
11
11
 
12
12
  /** The type of error. "MongooseError" for generic errors. */
package/types/index.d.ts CHANGED
@@ -178,6 +178,8 @@ declare module 'mongoose' {
178
178
  versionKey?: boolean;
179
179
  /** if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`. */
180
180
  flattenMaps?: boolean;
181
+ /** if true, convert any ObjectIds in the result to 24 character hex strings. */
182
+ flattenObjectIds?: boolean;
181
183
  /** If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema. */
182
184
  useProjection?: boolean;
183
185
  }
package/types/models.d.ts CHANGED
@@ -25,6 +25,7 @@ declare module 'mongoose' {
25
25
 
26
26
  interface MongooseBulkWriteOptions {
27
27
  skipValidation?: boolean;
28
+ throwOnValidationError?: boolean;
28
29
  }
29
30
 
30
31
  interface InsertManyOptions extends
@@ -34,6 +35,7 @@ declare module 'mongoose' {
34
35
  rawResult?: boolean;
35
36
  ordered?: boolean;
36
37
  lean?: boolean;
38
+ throwOnValidationError?: boolean;
37
39
  }
38
40
 
39
41
  type InsertManyResult<T> = mongodb.InsertManyResult<T> & {
@@ -202,5 +202,11 @@ declare module 'mongoose' {
202
202
  * @default { transform: true, flattenDecimals: true }
203
203
  */
204
204
  toObject?: ToObjectOptions;
205
+
206
+ /**
207
+ * If `true`, convert any aliases in filter, projection, update, and distinct
208
+ * to their database property names. Defaults to false.
209
+ */
210
+ translateAliases?: boolean;
205
211
  }
206
212
  }
package/types/query.d.ts CHANGED
@@ -161,6 +161,11 @@ declare module 'mongoose' {
161
161
  * timestamps. Does nothing if schema-level timestamps are not set.
162
162
  */
163
163
  timestamps?: boolean | QueryTimestampsConfig;
164
+ /**
165
+ * If `true`, convert any aliases in filter, projection, update, and distinct
166
+ * to their database property names. Defaults to false.
167
+ */
168
+ translateAliases?: boolean;
164
169
  upsert?: boolean;
165
170
  useBigInt64?: boolean;
166
171
  writeConcern?: mongodb.WriteConcern;