Skip to content

Commit

Permalink
Throw on unsupported fields projection
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava committed Dec 3, 2013
1 parent 5314ffe commit 001d281
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/minimongo/minimongo_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,18 @@ Tinytest.add("minimongo - fetch with fields", function (test) {
if (!i) return;
test.isTrue(x.i === arr[i-1].i + 1);
});

// Temporary unsupported operators
// queries are taken from MongoDB docs examples
test.throws(function () {
c.find({}, { fields: { 'grades.$': 1 } });
});
test.throws(function () {
c.find({}, { fields: { grades: { $elemMatch: { mean: 70 } } } });
});
test.throws(function () {
c.find({}, { fields: { grades: { $slice: [20, 10] } } });
});
});

Tinytest.add("minimongo - fetch with projection, subarrays", function (test) {
Expand Down
14 changes: 14 additions & 0 deletions packages/minimongo/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
// according to projection rules. Doesn't retain subfields
// of passed argument.
LocalCollection._compileProjection = function (fields) {
// XXX: $-operators are not supported in fields projections yet
if (! LocalCollection._supportedProjection(fields))
throw MinimongoError("Minimongo doesn't support fields projections "
+ "with $-operators yet");

var _idProjection = _.isUndefined(fields._id) ? true : fields._id;
var details = projectionDetails(fields);

Expand Down Expand Up @@ -159,3 +164,12 @@ pathsToTree = function (paths, newLeafFn, conflictFn, tree) {
return tree;
};

LocalCollection._supportedProjection = function (fields) {
return _.all(fields, function (val, keyPath) {
if (_.contains(keyPath.split('.'), '$'))
return false;
return !_.isObject(val) ||
(!_.has(val, '$slice') && !_.has(val, '$elemMatch'));
});
};

0 comments on commit 001d281

Please sign in to comment.