Replaced an _.each call with a Object.keys().forEach call, to allow objects with length properties to be handled properly as Mongo selectors. (#8380)

This commit is contained in:
Hugh Willson
2017-02-22 12:15:11 -05:00
committed by Ben Newman
parent 4cbcd9b008
commit a22793e443
2 changed files with 15 additions and 3 deletions

View File

@@ -378,7 +378,8 @@ Mongo.Collection._rewriteSelector = function (selector) {
return {_id: Random.id()};
var ret = {};
_.each(selector, function (value, key) {
Object.keys(selector).forEach((key) => {
const value = selector[key];
// Mongo supports both {field: /foo/} and {field: {$regex: /foo/}}
if (value instanceof RegExp) {
ret[key] = convertRegexpToMongoSelector(value);
@@ -388,8 +389,7 @@ Mongo.Collection._rewriteSelector = function (selector) {
// override the ones set on $regex.
if (value.$options !== undefined)
ret[key].$options = value.$options;
}
else if (_.contains(['$or','$and','$nor'], key)) {
} else if (_.contains(['$or','$and','$nor'], key)) {
// Translate lower levels of $and/$or/$nor
ret[key] = _.map(value, function (v) {
return Mongo.Collection._rewriteSelector(v);

View File

@@ -2198,6 +2198,18 @@ Tinytest.add('mongo-livedata - rewrite selector', function (test) {
var oid = new Mongo.ObjectID();
test.equal(Mongo.Collection._rewriteSelector(oid),
{_id: oid});
// Make sure selectors with "length" properties are handled properly
// (verifies issue #8329 has been resolved).
const SomeSelector = function (length) {
this.length = length;
};
const length = 2;
const testSelector = new SomeSelector(length);
test.equal(
Mongo.Collection._rewriteSelector(testSelector),
{ length }
);
});
testAsyncMulti('mongo-livedata - specified _id', [