Implement '$' update for $near

This commit is contained in:
David Glasser
2014-01-02 23:36:26 -08:00
parent e3e9cca12a
commit 2063999ce0
2 changed files with 16 additions and 8 deletions

View File

@@ -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});

View File

@@ -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;
};
}
};