Comparing version 0.7.6 to 0.7.7
@@ -226,3 +226,5 @@ /** | ||
// Set a field to a new value | ||
/** | ||
* Set a field to a new value | ||
*/ | ||
lastStepModifierFunctions.$set = function (obj, field, value) { | ||
@@ -232,3 +234,6 @@ obj[field] = value; | ||
// Push an element to the end of an array field | ||
/** | ||
* Push an element to the end of an array field | ||
*/ | ||
lastStepModifierFunctions.$push = function (obj, field, value) { | ||
@@ -242,5 +247,8 @@ // Create the array if it doesn't exist | ||
// Add an element to an array field only if it is not already in it | ||
// No modification if the element is already in the array | ||
// Note that it doesn't check whether the original array contains duplicates | ||
/** | ||
* Add an element to an array field only if it is not already in it | ||
* No modification if the element is already in the array | ||
* Note that it doesn't check whether the original array contains duplicates | ||
*/ | ||
lastStepModifierFunctions.$addToSet = function (obj, field, value) { | ||
@@ -254,3 +262,22 @@ // Create the array if it doesn't exist | ||
// Increment a numeric field's value | ||
/** | ||
* Remove the first or last element of an array | ||
*/ | ||
lastStepModifierFunctions.$pop = function (obj, field, value) { | ||
if (!util.isArray(obj[field])) { throw "Can't $pop an element from non-array values"; } | ||
if (typeof value !== 'number') { throw value + " isn't an integer, can't use it with $pop"; } | ||
if (value === 0) { return; } | ||
if (value > 0) { | ||
obj[field] = obj[field].slice(0, obj[field].length - 1); | ||
} else { | ||
obj[field] = obj[field].slice(1); | ||
} | ||
}; | ||
/** | ||
* Increment a numeric field's value | ||
*/ | ||
lastStepModifierFunctions.$inc = function (obj, field, value) { | ||
@@ -257,0 +284,0 @@ if (typeof value !== 'number') { throw value + " must be a number"; } |
{ | ||
"name": "nedb", | ||
"version": "0.7.6", | ||
"version": "0.7.7", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "tldr.io", |
@@ -205,3 +205,3 @@ # NeDB (Node embedded database) | ||
* A new document will replace the matched docs | ||
* The available modifiers are `$set` to change a field's value and `$inc` to increment a field's value. The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. See examples below for the syntax | ||
* The available modifiers are `$set` to change a field's value, `$inc` to increment a field's value and `$push`, `$pop`, `$addToSet` to work on arrays. The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. See examples below for the syntax | ||
* `options` is an object with two possible parameters | ||
@@ -263,2 +263,23 @@ * `multi` (defaults to `false`) which allows the modification of several documents if set to true | ||
}); | ||
// If we insert a new document { _id: 'id6', fruits: ['apple', 'orange', 'pear'] } in the collection, | ||
// let's see how we can modify the array field atomically | ||
// $push inserts new elements at the end of the array | ||
db.update({ _id: 'id6' }, { $push: { fruits: ['banana'] } }, {}, function () { | ||
// Now the fruits array is ['apple', 'orange', 'pear', 'banana'] | ||
}); | ||
// $pop removes an element from the end (if used with 1) or the front (if used with -1) of the array | ||
db.update({ _id: 'id6' }, { $pop: { fruits: 1 } }, {}, function () { | ||
// Now the fruits array is ['apple', 'orange'] | ||
// With { $pop: { fruits: -1 } }, it would have been ['orange', 'pear'] | ||
}); | ||
// $addToSet adds an element to an array only if it isn't already in it | ||
// Note that it doesn't check whether the array contained duplicates before or not | ||
db.update({ _id: 'id6' }, { $addToSet: { fruits: ['apple'] } }, {}, function () { | ||
// The fruits array didn't change | ||
// If we had used a fruit not in the array, e.g. 'banana', it would have been added to the array | ||
}); | ||
``` | ||
@@ -265,0 +286,0 @@ |
@@ -414,2 +414,45 @@ var model = require('../lib/model') | ||
describe('$pop modifier', function () { | ||
it('Throw if called on a non array, a non defined field or a non integer', function () { | ||
var obj = { arr: 'hello' } | ||
, modified; | ||
(function () { | ||
modified = model.modify(obj, { $pop: { arr: 1 } }); | ||
}).should.throw(); | ||
obj = { bloup: 'nope' }; | ||
(function () { | ||
modified = model.modify(obj, { $pop: { arr: 1 } }); | ||
}).should.throw(); | ||
obj = { arr: [1, 4, 8] }; | ||
(function () { | ||
modified = model.modify(obj, { $pop: { arr: true } }); | ||
}).should.throw(); | ||
}); | ||
it('Can remove the first and last element of an array', function () { | ||
var obj | ||
, modified; | ||
obj = { arr: [1, 4, 8] }; | ||
modified = model.modify(obj, { $pop: { arr: 1 } }); | ||
assert.deepEqual(modified, { arr: [1, 4] }); | ||
obj = { arr: [1, 4, 8] }; | ||
modified = model.modify(obj, { $pop: { arr: -1 } }); | ||
assert.deepEqual(modified, { arr: [4, 8] }); | ||
// Empty arrays are not changed | ||
obj = { arr: [] }; | ||
modified = model.modify(obj, { $pop: { arr: 1 } }); | ||
assert.deepEqual(modified, { arr: [] }); | ||
modified = model.modify(obj, { $pop: { arr: -1 } }); | ||
assert.deepEqual(modified, { arr: [] }); | ||
}); | ||
}); // End of '$pop modifier' | ||
}); // ==== End of 'Modifying documents' ==== // | ||
@@ -416,0 +459,0 @@ |
217614
4653
400