-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed; prohibit updating arrays selected with $elemMatch
An $elemMatch projection only returns the elements of the array in the database that match the argument. While helpful when working with large arrays, it means we can no longer depend on any array elements position being accurate - manipulating these arrays with the mongoose helper methods would potentially result in incorrect array elements getting updated or even loss of array elements in the database. Attempting to update an array selected with an $elemMatch projection now returns an error that includes the array paths in violation. To update arrays selected with $elemMatch, manually use Model.update, Model.findByIdAndUpdate, or another method outside of document.save(). closes #1334
- Loading branch information
Showing
4 changed files
with
127 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
/*! | ||
* Module dependencies. | ||
*/ | ||
|
||
var MongooseError = require('../error'); | ||
|
||
/*! | ||
* ElemMatch Error constructor. | ||
* | ||
* @inherits MongooseError | ||
*/ | ||
|
||
function ElemMatchError (paths) { | ||
var msg = 'Using `document.save()` to update an array which was selected ' | ||
+ 'using an $elemMatch projection is not supported. The following ' | ||
+ 'path(s) were were selected with an $elemMatch projection:\n' | ||
+ ' ' + paths.join('\n ') + '\n' | ||
+ 'Use Model.update() to update these arrays instead.' | ||
|
||
MongooseError.call(this, msg); | ||
Error.captureStackTrace(this, arguments.callee); | ||
this.name = 'ElemMatchError'; | ||
}; | ||
|
||
/*! | ||
* Inherits from MongooseError. | ||
*/ | ||
|
||
ElemMatchError.prototype.__proto__ = MongooseError.prototype; | ||
|
||
/*! | ||
* exports | ||
*/ | ||
|
||
module.exports = ElemMatchError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters