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

MongoDB Query: Conditon operators “||” and “&&” returns unexpected result

    XMLWordPrintableJSON

Details

    • Icon: Question Question
    • Resolution: Done
    • Icon: Minor - P4 Minor - P4
    • None
    • None
    • Shell
    • None

    Description

      Hi

      I’m seeing an unexpected result while querying with “||” and “”&&” conditioning operations. Looks like this is a bug.

      Database records:
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      > db.cars.find()
      { "_id" : 100, "available" : 1, "color" : "red", "name" : "GTO", "year" : 3939 }
       
      Below commands works as expected:
      > db.cars.find( {available : 1, year : 3939})
      { "_id" : 100, "available" : 1, "color" : "red", "name" : "GTO", "year" : 3939 }
       
      >
      > db.cars.find( { $where: "this.available == 1" && "this.year == 3939" })
      { "_id" : 100, "available" : 1, "color" : "red", "name" : "GTO", "year" : 3939 }
       
      >
      >
      > db.cars.find( { $where: "this.available == 1" || "this.year == 3939" })
      { "_id" : 100, "available" : 1, "color" : "red", "name" : "GTO", "year" : 3939 }
       
       
      Problem seen:
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
      > ####  The below command uses OR condition operation (i.e using || ) and only one condition is true. But still the entry is not displayed.. This is incorrect.
      > db.cars.find( { $where: "this.available == 11" || "this.year == 3939" })
       
      >#### The below command uses AND condition operation (i.e using && ) and only one condition is true, but entry is displayed which is incorrect. For AND condition both the expression should return TRUE.
      > db.cars.find( { $where: "this.available == 11" && "this.year == 3939" })
      { "_id" : 100, "available" : 1, "color" : "red", "name" : "GTO", "year" : 3939 }

      Please let me know any clarification required by sending mail to jayanna.hallur@wipro.com

      Regards
      Jayanna Hallur
      Big Data Analyst,
      Wipro Technologies, Bangalore
      jayanna.hallur@wipro.com

      Attachments

        Activity

          People

            scotthernandez Scott Hernandez (Inactive)
            Jayannah Jayanna Hallur [X]
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: