From ca2cce8ab6ce58577f83c3bf7f6787579b4a338d Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Fri, 21 Apr 2017 15:05:08 -0700 Subject: [PATCH] Fix exception when adding and removing tags Only happens if adding and removing tags within the same session. Resolves: #229 --- lib/db.coffee | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/db.coffee b/lib/db.coffee index cb7aa80..efaa21c 100644 --- a/lib/db.coffee +++ b/lib/db.coffee @@ -445,10 +445,18 @@ saveDevice = (deviceId, deviceData, isNew, callback) -> if (update['$unset']?) optimizeProjection(update['$unset']) + if update['$addToSet'] and update['$pull'] + # Mongo doesn't allow $addToSet and $pull at the same time + update2 = {'$pull': update['$pull']} + delete update['$pull'] + devicesCollection.update({'_id' : deviceId}, update, {upsert: isNew}, (err, result) -> if not err and result.result.n != 1 return callback(new Error("Device #{deviceId} not found in database")) + if update2 + return devicesCollection.update({'_id': deviceId}, update2, callback) + return callback(err) )