Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: $all and $elemMatch fails on nested elements #1

Closed
steffenmllr opened this issue Nov 24, 2013 · 2 comments
Closed

BUG: $all and $elemMatch fails on nested elements #1

steffenmllr opened this issue Nov 24, 2013 · 2 comments
Assignees

Comments

@steffenmllr
Copy link

Hey there. first off: awesome stuff you did with mingo. However I encountered a bug while querying a nested array using $all and $elemMatch. I setup a testcase to illustrate the issue:

test("elemMatch on nested elements", function () {

  var testobject = [{
      user: { username: 'User1', projects: [{ name: "Project 1", rating: { complexity: 6 }}, { name: "Project 2", rating: { complexity: 2 }}] }
  },{
      user: { username: 'User2', projects: [{ name: "Project 1", rating: { complexity: 6 }}, { name: "Project 2", rating: { complexity: 8 }}] }
  }],
  query = {
    'user.projects': {
      "$all": [{
        "$elemMatch": {
          'rating.complexity': { '$gt' : 6 }
        }
      }]
    }
  };
  // It should return one user object
  ok(Mingo.compile(query).test(testobject));

});

Expected behavior: http://docs.mongodb.org/manual/reference/operator/query/all/#use-all-with-elemmatch

@steffenmllr
Copy link
Author

Awesome. I'll check if it solves my problems. Btw. do you know https://github.com/meteor/meteor/blob/devel/packages/minimongo and https://github.com/louischatriot/nedb ?

It's another implementation of the same thing

@kofrasa
Copy link
Owner

kofrasa commented Nov 24, 2013

Hi Steffen, am glad you find mingo useful and thanks for the report. I have resolved this bug and made a few corrections to your test case. The test function works per object instead of a collection. The find function is more appropriate instead.

var data = [{
      user: { username: 'User1', projects: [{ name: "Project 1", rating: { complexity: 6 }}, { name: "Project 2", rating: { complexity: 2 }}] }
    },{
      user: { username: 'User2', projects: [{ name: "Project 1", rating: { complexity: 6 }}, { name: "Project 2", rating: { complexity: 8 }}] }
    }];
    var query = {
      'user.projects': {
        "$all": [{
          "$elemMatch": {
            'rating.complexity': { '$gt' : 6 }
          }
        }]
      }
    };
    // It should return one user object
    var result = Mingo.find(data, query).count();
    equal(1, result, "can match using $all with $elemMatch on nested elements");

see commit f417c95

@kofrasa kofrasa closed this as completed Nov 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants