From 578c60ab7984d4634146294d2245d8d27cafc063 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 25 May 2013 14:47:03 +0200 Subject: [PATCH] Better way to update --- lib/datastore.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 871e06f..33c1992 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -240,9 +240,8 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { , self = this , numReplaced = 0 , multi, upsert - , modifiedDoc - , newData = [] , updatedDocs = [] + , i ; if (typeof options === 'function') { cb = options; options = {}; } @@ -270,24 +269,19 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { } , function () { // Perform the update try { - self.data.forEach(function (d) { - if (model.match(d, query) && (multi || numReplaced === 0)) { + for (i = 0; i < self.data.length; i += 1) { + if (model.match(self.data[i], query) && (multi || numReplaced === 0)) { numReplaced += 1; - modifiedDoc = model.modify(d, updateQuery); - - newData.push(modifiedDoc); - updatedDocs.push(modifiedDoc); - } else { - newData.push(d); + self.data[i] = model.modify(self.data[i], updateQuery); + updatedDocs.push(self.data[i]); } - }); + } } catch (err) { return callback(err); } self.persistNewState(updatedDocs, function (err) { if (err) { return callback(err); } - self.data = newData; return callback(null, numReplaced); }); }