You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
0 commit comments