diff --git a/README.md b/README.md index 77b909fb..fc8c7f4a 100755 --- a/README.md +++ b/README.md @@ -415,7 +415,7 @@ db.count({}, function (err, count) { * `query` is the same kind of finding query you use with `find` and `findOne` * `update` specifies how the documents should be modified. It is either a new document or a set of modifiers (you cannot use both together, it doesn't make sense!) * A new document will replace the matched docs - * The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. Available field modifiers are `$set` to change a field's value, `$unset` to delete a field and `$inc` to increment a field's value. To work on arrays, you have `$push`, `$pop`, `$addToSet`, `$pull`, and the special `$each`. See examples below for the syntax. + * The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. Available field modifiers are `$set` to change a field's value, `$unset` to delete a field and `$inc` to increment a field's value. To work on arrays, you have `$push`, `$pop`, `$addToSet`, `$pull`, and the special `$each` and `$slice`. See examples below for the syntax. * `options` is an object with two possible parameters * `multi` (defaults to `false`) which allows the modification of several documents if set to true * `upsert` (defaults to `false`) if you want to insert a new document corresponding to the `update` rules if your `query` doesn't match anything. If your `update` is a simple object with no modifiers, it is the inserted document. In the other case, the `query` is stripped from all operator recursively, and the `update` is applied to it. @@ -510,13 +510,19 @@ db.update({ _id: 'id6' }, { $pull: { fruits: $in: ['apple', 'pear'] } }, {}, fun // Now the fruits array is ['orange'] }); - - // $each can be used to $push or $addToSet multiple values at once // This example works the same way with $addToSet -db.update({ _id: 'id6' }, { $push: { fruits: {$each: ['banana', 'orange'] } } }, {}, function () { +db.update({ _id: 'id6' }, { $push: { fruits: { $each: ['banana', 'orange'] } } }, {}, function () { // Now the fruits array is ['apple', 'orange', 'pear', 'banana', 'orange'] }); + +// $slice can be used in cunjunction with $push and $each to limit the size of the resulting array. +// A value of 0 will update the array to an empty array. A positive value n will keep only the n first elements +// A negative value -n will keep only the last n elements. +// If $slice is specified but not $each, $each is set to [] +db.update({ _id: 'id6' }, { $push: { fruits: { $each: ['banana'], $slice: 2 } } }, {}, function () { + // Now the fruits array is ['apple', 'orange'] +}); ``` ### Removing documents