Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-14010

Use of $where within $elemMatch

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 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.

      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@mongodb.com David Storch
            Reporter:
            neillunn Neil Lunn
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: