Anonymous Person
Anonymous Person

Reputation: 1538

MongoDB OR operation questions

I am learning MongoDB myself, and I find myself in a weird situation. Here's the query #1

  1. When I run db.players.find({$or: [{"position":"Left Wing"}, {"position":"Right Wing"}]}).pretty(), on my sample dataset, this is the result:

    "_id" : ObjectId("57d9a212698d3e0bd0f5a245"),
    "position" : "Right Wing",
    "id" : 8465166,
    "weight" : 200,
    "height" : "6' 0\"",
    "imageUrl" : "http://1.cdn.nhle.com/photos/mugs/8465166.jpg",
    "birthplace" : "Seria, BRN",
    "age" : 37,
    "name" : "Craig Adams",
    "birthdate" : "April 26, 1977",
    "number" : 27
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a246"),
    "position" : "Right Wing",
    "id" : 8475761,
    "weight" : 195,
    "height" : "6' 2\"",
    "imageUrl" : "http://1.cdn.nhle.com/photos/mugs/8475761.jpg",
    "birthplace" : "Gardena, CA, USA",
    "age" : 23,
    "name" : "Beau Bennett",
    "birthdate" : "November 27, 1991",
    "number" : 19
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a247"),
    "position" : "Left Wing",
    "id" : 8471260,
    "weight" : 202,
    "height" : "6' 1\"",
    "imageUrl" : "http://3.cdn.nhle.com/photos/mugs/8471260.jpg",
    "birthplace" : "Meadow Lake, SK, CAN",
    "age" : 29,
    "name" : "Blake Comeau",
    "birthdate" : "February 18, 1986",
    "number" : 17
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a249"),
    "position" : "Right Wing",
    "id" : 8471703,
    "twitterURL" : "https://twitter.com/S9Downie",
    "weight" : 191,
    "height" : "5' 11\"",
    "imageUrl" : "http://1.cdn.nhle.com/photos/mugs/8471703.jpg",
    "birthplace" : "Newmarket, ON, CAN",
    "twitterHandle" : "S9Downie",
    "age" : 28,
    "name" : "Steve Downie",
    "birthdate" : "April 03, 1987",
    "number" : 23
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a24a"),
    "position" : "Right Wing",
    "id" : 8466393,
    "weight" : 205,
    "height" : "6' 1\"",
    "imageUrl" : "http://2.cdn.nhle.com/photos/mugs/8466393.jpg",
    "birthplace" : "Laval, QC, CAN",
    "age" : 35,
    "name" : "Pascal Dupuis",
    "birthdate" : "April 07, 1979",
    "number" : 9
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a24b"),
    "position" : "Right Wing",
    "id" : 8471887,
    "weight" : 189,
    "height" : "5' 11\"",
    "imageUrl" : "http://1.cdn.nhle.com/photos/mugs/8471887.jpg",
    "birthplace" : "Sollentuna, SWE",
    "age" : 28,
    "name" : "Patric Hornqvist",
    "birthdate" : "January 01, 1987",
    "number" : 72
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a24c"),
    "position" : "Left Wing",
    "id" : 8470543,
    "weight" : 195,
    "height" : "6' 0\"",
    "imageUrl" : "http://3.cdn.nhle.com/photos/mugs/8470543.jpg",
    "birthplace" : "Regina, SK, CAN",
    "age" : 35,
    "name" : "Chris Kunitz",
    "birthdate" : "September 26, 1979",
    "number" : 14
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a24f"),
    "position" : "Left Wing",
    "id" : 8474102,
    "twitterURL" : "https://twitter.com/DP_57",
    "weight" : 200,
    "height" : "6' 0\"",
    "imageUrl" : "http://3.cdn.nhle.com/nhl/en/v3/photos/mugs/8474102.jpg",
    "birthplace" : "Sherbrooke, QC, CAN",
    "twitterHandle" : "DP_57",
    "age" : 26,
    "name" : "David Perron",
    "birthdate" : "May 28, 1988",
    "number" : 39
    

    Sorry, formatting a JSON file is difficult. Please assume the larger gaps above as where the brackets were.

  2. When I run db.players.find({"position":"Left Wing"} || {"position":"Right Wing"}).pretty(), the result is:

    "_id" : ObjectId("57d9a212698d3e0bd0f5a247"),
    "position" : "Left Wing",
    "id" : 8471260,
    "weight" : 202,
    "height" : "6' 1\"",
    "imageUrl" : "http://3.cdn.nhle.com/photos/mugs/8471260.jpg",
    "birthplace" : "Meadow Lake, SK, CAN",
    "age" : 29,
    "name" : "Blake Comeau",
    "birthdate" : "February 18, 1986",
    "number" : 17
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a24c"),
    "position" : "Left Wing",
    "id" : 8470543,
    "weight" : 195,
    "height" : "6' 0\"",
    "imageUrl" : "http://3.cdn.nhle.com/photos/mugs/8470543.jpg",
    "birthplace" : "Regina, SK, CAN",
    "age" : 35,
    "name" : "Chris Kunitz",
    "birthdate" : "September 26, 1979",
    "number" : 14
    
    
    "_id" : ObjectId("57d9a212698d3e0bd0f5a24f"),
    "position" : "Left Wing",
    "id" : 8474102,
    "twitterURL" : "https://twitter.com/DP_57",
    "weight" : 200,
    "height" : "6' 0\"",
    "imageUrl" : "http://3.cdn.nhle.com/nhl/en/v3/photos/mugs/8474102.jpg",
    "birthplace" : "Sherbrooke, QC, CAN",
    "twitterHandle" : "DP_57",
    "age" : 26,
    "name" : "David Perron",
    "birthdate" : "May 28, 1988",
    "number" : 39
    

Why's this discrepancy, considering they're both OR operation? Please help me understand it.

Windows 10/MongoDB on IntelliJ

Upvotes: 1

Views: 57

Answers (2)

JohnnyHK
JohnnyHK

Reputation: 311905

The parameter of {"position":"Left Wing"} || {"position":"Right Wing"} is a JavaScript expression that evaluates to {"position":"Left Wing"} because that term is truthy. So that's what you're actually passing into find, and that's why you're only getting the "Left Wing" documents in that query.

You can see this in the shell by doing:

> var query = {"position":"Left Wing"} || {"position":"Right Wing"}
> query
{ "position" : "Left Wing" }

But you can also simplify your $or query to use $in instead:

db.players.find({"position": {$in: ["Left Wing", "Right Wing"]}})

Upvotes: 1

ranafeb14
ranafeb14

Reputation: 437

As per my understanding with little mongo knowledge.

  • || operator command works in the below manner

  • a || b -> if a is true it just passes value a to the db.players.find(a) and ignores b.

  • here in your case 'a' refers to get the {"position":"Left Wing"} so it retrieves only "Left Wing" documents.

  • where as $or: prepares indexes on each condition and fetches the combined results for "Left Wing" and "Right Wing".

Try the below commands on mongo shell, I think it gives some better idea.

> {$or: [(1==1), (2==1)]}
 [ true, false ]
> (1==1) || (2==1)
 true
> {$or: [(2==1), (1==1)]}
 [ false, true ]
> (2==1) || (1==1)
 true

Hope it helps.

Thanks,
Rana

Upvotes: 1

Related Questions