[SERVER-11078] MongoDB Query: Conditon operators “||” and “&&” returns unexpected result Created: 08/Oct/13  Updated: 08/Oct/13  Resolved: 08/Oct/13

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Minor - P4
Reporter: Jayanna Hallur [X] Assignee: Scott Hernandez (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OS Type : Window 7, 32 bit machine

Version:

C:\mongodb\bin>mongo.exe -version MongoDB shell version: 2.4.6

C:\mongodb\bin> C:\mongodb\bin>mongod.exe -version db version v2.4.6 Tue Oct 08 16:08:54.073 git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673


Participants:

 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



 Comments   
Comment by Scott Hernandez (Inactive) [ 08/Oct/13 ]

You need to put the "&&" "||" inside the quotes. What you have now is evaluated by javascript as && of two strings:

Incorrect:

> query = {$where: "this.available == 11" || "this.year == 3939"};
{ "$where" : "this.available == 11" }
> query = {$where: "this.available == 11" && "this.year == 3939"};
{ "$where" : "this.year == 3939" }

Correct:

> query = {$where: "this.available == 11 || this.year == 3939"};
{ "$where" : "this.available == 11 || this.year == 3939" }

Generated at Thu Feb 08 03:24:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.