Use of $where within $elemMatch

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.4.9, 2.4.10, 2.6.0, 2.6.1
    • Component/s: JavaScript, Querying
    • None
    • ALL
    • Hide

      Set up a document with a sub-document in an array:

          db.docs.insert({
              "data": [{ "name": "Kimberly" }]
          })
      

      Attempt a JavaScript match as done by the poster:

          db.docs.find({
              "data": {
                  "$elemMatch":{
                       "$where" : "this.name.toLowerCase().indexOf('kim') ==0"
                  }
              }
          });
      

      Produces the error in 2.6:

      error: {
              "$err" : "Can't canonicalize query: BadValue $elemMatch cannot contain $where expression",
              "code" : 17287
      }
      

      Which clearly seems intentional, but in prior versions the query produces a matching result without throwing the error.

      Show
      Set up a document with a sub-document in an array: db.docs.insert({ "data" : [{ "name" : "Kimberly" }] }) Attempt a JavaScript match as done by the poster: db.docs.find({ "data" : { "$elemMatch" :{ "$where" : " this .name.toLowerCase().indexOf( 'kim' ) ==0" } } }); Produces the error in 2.6: error: { "$err" : "Can't canonicalize query: BadValue $elemMatch cannot contain $where expression" , "code" : 17287 } Which clearly seems intentional, but in prior versions the query produces a matching result without throwing the error.
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None

      Different behavior of $where within $elemMatch from 2.4 version to 2.6 the issue was raised in this question

      It is noted to the poster that they should not be using the type of query as shown as the results can be achieved by other means.

      This probably requires a documentation patch to cover the changed behavior.

            Assignee:
            David Storch
            Reporter:
            Neil Lunn
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: