-
-
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; saving divergent populated arrays
When a $set of an entire array or a $pop operation of that array is produced through document.save() and the array was populated using limit, skip, query condition, or deselection of the _id field, we now return an error. The view mongoose has of the array has diverged from the array in the database and these operations would have unknown consequences on that data. $pop: // database { _id: 1, array: [3,5,7,9] } // mongoose var query = Doc.findOne(); query.populate({ path: 'array', match: { name: 'three' }}) query.exec(function (err, doc) { console.log(doc.array) // [{ _id: 3, name: 'three' }] doc.$pop(); console.log(doc.array) // [] doc.save() // <== Error }) This $pop removed the document with the _id of 3 from the array locally but when sent to the database would have removed the number 9 (since $pop removes the last element of the array). Instead, one could use doc.array.pull({ _id: 3 }) or perform this update manually using doc.update(..); $set: // database { _id: 1, array: [9,3,7,5] } // mongoose var query = Doc.findOne(); query.populate({ path: 'array', match: { _id: { $gt: 7 }}}) query.exec(function (err, doc) { console.log(doc.array) // [{ _id: 9 }] doc.array.unshift({ _id: 2 }) console.log(doc.array) // [{ _id: 2 }, { _id: 9 }] doc.save() // <== Error }) The would result in a $set of the entire array (there is no equivalent atomic operator for `unshift`) which would overwrite the other un-matched elements in the array in the database. Use doc.update() instead.
- Loading branch information
Showing
7 changed files
with
290 additions
and
72 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,40 @@ | ||
|
||
/*! | ||
* Module dependencies. | ||
*/ | ||
|
||
var MongooseError = require('../error'); | ||
|
||
/*! | ||
* DivergentArrayError constructor. | ||
* | ||
* @inherits MongooseError | ||
*/ | ||
|
||
function DivergentArrayError (paths) { | ||
var msg = 'For your own good, using `document.save()` to update an array ' | ||
+ 'which was selected using an $elemMatch projection OR ' | ||
+ 'populated using skip, limit, query conditions, or exclusion of ' | ||
+ 'the _id field when the operation results in a $pop or $set of ' | ||
+ 'the entire array is not supported. The following ' | ||
+ 'path(s) would have been modified unsafely:\n' | ||
+ ' ' + paths.join('\n ') + '\n' | ||
+ 'Use Model.update() to update these arrays instead.' | ||
// TODO write up a docs page (FAQ) and link to it | ||
|
||
MongooseError.call(this, msg); | ||
Error.captureStackTrace(this, arguments.callee); | ||
this.name = 'DivergentArrayError'; | ||
}; | ||
|
||
/*! | ||
* Inherits from MongooseError. | ||
*/ | ||
|
||
DivergentArrayError.prototype.__proto__ = MongooseError.prototype; | ||
|
||
/*! | ||
* exports | ||
*/ | ||
|
||
module.exports = DivergentArrayError; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.