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

Use of $where within $elemMatch

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 2.4.9, 2.4.10, 2.6.0, 2.6.1
    • Fix Version/s: None
    • Component/s: JavaScript, Querying
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      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.

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              david.storch David Storch
              Reporter:
              neillunn Neil Lunn
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: