mongoose 8.24.1 → 8.24.3

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
@@ -2787,12 +2787,7 @@ Query.prototype.findOne = function(conditions, projection, options) {
2787
2787
 
2788
2788
  Query.prototype._countDocuments = async function _countDocuments() {
2789
2789
  this._applyTranslateAliases();
2790
-
2791
- try {
2792
- this.cast(this.model);
2793
- } catch (err) {
2794
- this.error(err);
2795
- }
2790
+ this._castConditions();
2796
2791
 
2797
2792
  if (this.error()) {
2798
2793
  throw this.error();
@@ -5204,11 +5199,17 @@ Query.prototype.cursor = function cursor(opts) {
5204
5199
  }
5205
5200
 
5206
5201
  try {
5207
- this.cast(this.model);
5202
+ this._castConditions();
5208
5203
  } catch (err) {
5204
+ // `_castConditions()` reports cast errors on the query, but `sanitizeFilter`
5205
+ // throws for filters it refuses outright, like `$where`.
5209
5206
  return (new QueryCursor(this))._markError(err);
5210
5207
  }
5211
5208
 
5209
+ if (this.error()) {
5210
+ return (new QueryCursor(this))._markError(this.error());
5211
+ }
5212
+
5212
5213
  return new QueryCursor(this);
5213
5214
  };
5214
5215
 
@@ -452,7 +452,9 @@ if (util.inspect.custom) {
452
452
 
453
453
  /**
454
454
  * Override `$toObject()` to handle minimizing the whole path. Should not minimize if schematype-level minimize
455
- * is set to false re: gh-11247, gh-14058, gh-14151
455
+ * is set to false re: gh-11247, gh-14058, gh-14151. Should not minimize document array elements: an array
456
+ * element cannot be removed without shifting the array, so minimizing it to `undefined` makes the BSON
457
+ * serializer store `null` re: gh-7322.
456
458
  */
457
459
 
458
460
  Subdocument.prototype.$toObject = function $toObject(options, json) {
@@ -462,7 +464,7 @@ Subdocument.prototype.$toObject = function $toObject(options, json) {
462
464
  // If minimize is set, then we can minimize out the whole object.
463
465
  if (Object.keys(ret).length === 0 && options?._calledWithOptions != null) {
464
466
  const minimize = options._calledWithOptions?.minimize ?? this?.$__schemaTypeOptions?.minimize ?? options.minimize;
465
- if (minimize && !this.constructor.$__required) {
467
+ if (minimize && !this.constructor.$__required && !this.$isDocumentArrayElement) {
466
468
  return undefined;
467
469
  }
468
470
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.24.1",
4
+ "version": "8.24.3",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
package/types/models.d.ts CHANGED
@@ -660,11 +660,11 @@ declare module 'mongoose' {
660
660
  */
661
661
  useConnection(connection: Connection): this;
662
662
 
663
- /** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */
664
- validate(): Promise<void>;
665
- validate(obj: any): Promise<void>;
666
- validate(obj: any, pathsOrOptions: PathsToValidate): Promise<void>;
667
- validate(obj: any, pathsOrOptions: { pathsToSkip?: pathsToSkip }): Promise<void>;
663
+ /** Casts and validates the given object against this model's schema, returning the casted-and-validated copy of `obj`, passing the given `context` to custom validators. */
664
+ validate(): Promise<TRawDocType>;
665
+ validate(obj: any): Promise<TRawDocType>;
666
+ validate(obj: any, pathsOrOptions: PathsToValidate): Promise<TRawDocType>;
667
+ validate(obj: any, pathsOrOptions: { pathsToSkip?: pathsToSkip }): Promise<TRawDocType>;
668
668
 
669
669
  /** Watches the underlying collection for changes using [MongoDB change streams](https://www.mongodb.com/docs/manual/changeStreams/). */
670
670
  watch<ResultType extends mongodb.Document = any, ChangeType extends mongodb.ChangeStreamDocument = any>(pipeline?: Array<Record<string, unknown>>, options?: mongodb.ChangeStreamOptions & { hydrate?: boolean }): mongodb.ChangeStream<ResultType, ChangeType>;