MongoDB OR操作问题

时间:2016-09-14 19:43:09

标签: mongodb or-operator

我自己正在学习MongoDB,我发现自己处于一种奇怪的境地。这是查询#1

  1. 当我在我的样本数据集上运行db.players.find({$or: [{"position":"Left Wing"}, {"position":"Right Wing"}]}).pretty()时,结果如下:

    "_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
    

    抱歉,格式化JSON文件很困难。请假设上方较大的间隙与括号所在的位置相同。

  2. 当我运行db.players.find({"position":"Left Wing"} || {"position":"Right Wing"}).pretty(),时,结果为:

    "_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
    
  3. 为什么这种差异,考虑到它们都是OR操作?请帮我理解。

    IntelliJ上的Windows 10 / MongoDB

2 个答案:

答案 0 :(得分:1)

根据我对少量mongo知识的理解。

  • || operator命令的工作方式如下

  • a || b - >如果a为true,则只将值a传递给db.players.find(a)并忽略b。

  • 在您的情况下' a'指的是{"位置":"左翼"}所以它只检索"左翼"文档。

  • 其中$或:为每个条件准备索引并获取" Left Wing"的结合结果。和"右翼"。

在mongo shell上尝试以下命令,我认为它提供了一些更好的主意。

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

希望它有所帮助。

<强>谢谢,

答案 1 :(得分:1)

{"position":"Left Wing"} || {"position":"Right Wing"}的参数是一个评估为{"position":"Left Wing"}的JavaScript表达式,因为该术语是真实的。这就是您实际传入find的内容,这就是为什么您只在该查询中获取"Left Wing"文档。

您可以通过执行以下操作在shell中看到此内容:

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

但您也可以简化$or查询以改为使用$in

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