From 2063999ce05df92bad420ebb63e6db5cd56b4fa2 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Thu, 2 Jan 2014 23:36:26 -0800 Subject: [PATCH] Implement '$' update for $near --- packages/minimongo/minimongo_tests.js | 7 +++++++ packages/minimongo/selector.js | 17 +++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/minimongo/minimongo_tests.js b/packages/minimongo/minimongo_tests.js index c75099b765..c392c46be5 100644 --- a/packages/minimongo/minimongo_tests.js +++ b/packages/minimongo/minimongo_tests.js @@ -1805,6 +1805,13 @@ Tinytest.add("minimongo - modify", function (test) { {'a.x': 1, 'a.y': 3}, {$set: {'a.$.z': 5}}, {a: [{x: 1}, {y: 3, z: 5}]}); + // with $near, make sure it finds the closest one + modifyWithQuery({a: [{b: [1,1]}, + {b: [ [3,3], [4,4] ]}, + {b: [9,9]}]}, + {'a.b': {$near: [5, 5]}}, + {$set: {'a.$.b': 'k'}}, + {a: [{b: [1,1]}, {b: 'k'}, {b: [9,9]}]}); // $inc modify({a: 1, b: 2}, {$inc: {a: 10}}, {a: 11, b: 2}); diff --git a/packages/minimongo/selector.js b/packages/minimongo/selector.js index 488d18ea65..2b746cb1ba 100644 --- a/packages/minimongo/selector.js +++ b/packages/minimongo/selector.js @@ -437,22 +437,23 @@ var VALUE_OPERATORS = { // actually show up *multiple times* in the result set, with one entry for // each within-$maxDistance branching point. branchedValues = expandArraysInBranches(branchedValues); - var minDistance = null; + var result = {result: false}; _.each(branchedValues, function (branch) { var curDistance = distance(branch.value); // Skip branches that aren't real points or are too far away. if (curDistance === null || curDistance > maxDistance) return; // Skip anything that's a tie. - if (minDistance !== null && minDistance <= curDistance) + if (result.distance !== undefined && result.distance <= curDistance) return; - minDistance = curDistance; + result.result = true; + result.distance = curDistance; + if (branch.arrayIndex === undefined) + delete result.arrayIndex; + else + result.arrayIndex = branch.arrayIndex; }); - if (minDistance !== null) { - // XXX arrayIndex! - return {result: true, distance: minDistance}; - } - return {result: false}; + return result; }; } };