2015-04-01 37 views
1

我已经运行了以下实验,比较如何MongoDB的2.4和MongoDB 2.6关于$geoWithin选择与$not与多边形组合(即“外部多边形”查询)的行为。我包括特定的版本(三个数字),alghouth,我想它会发生与2.4和2.6的其他次要版本相同。

在给定的集合中创建两个文档(A和B):A,其中p字段设置为坐标[1,1],而B没有p字段。接下来,我在p中创建一个2dsphere索引,并对顶点为[0,0],[0,4]和[4,0]的三角形以外的区域进行查询。请注意,A是里面的这个多边形(所以它不应该被这个查询得到)。

随着2.4.9:

db.x.insert({k: "A", p: [1,1]}) 
db.x.insert({k: "B"}) 
db.x.ensureIndex({"p": "2dsphere"}) 
db.x.find({p: { $not: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [ [ [ 0, 0 ], [ 0, 4 ], [ 4, 0 ], [ 0, 0 ] ] ] } } } }}) 
--> no result 

有道理:不返回A(因为它是多边形内部)并且不返回B(假定它不具有p字段)。

接下来,2.6.1测试相同的脚本:

db.x.insert({k: "A", p: [1,1]}) 
db.x.insert({k: "B"}) 
db.x.ensureIndex({"p": "2dsphere"}) 
db.x.find({p: { $not: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [ [ [ 0, 0 ], [ 0, 4 ], [ 4, 0 ], [ 0, 0 ] ] ] } } } }}) 
-> result: B 

看来,在2.6语义发生了变化,所以当2dsphere索引的字段不是一个给定的文件中,该文件被认为是外任何可能的多边形。

只要新版本中的某种机制允许以旧方式配置行为,就可以在版本之间更改语义。我认为这个机制在创建索引时使用{ "2dsphereIndexVersion" : 1 }(基于我读的here,也许我误解了这些信息......)。但是,结果(2.6.1再次)是相同的:

db.x.insert({k: "A", p: [1,1]}) 
db.x.insert({k: "B"}) 
db.x.ensureIndex({"p": "2dsphere"}, { "2dsphereIndexVersion" : 1 }) 
db.x.find({p: { $not: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [ [ [ 0, 0 ], [ 0, 4 ], [ 4, 0 ], [ 0, 0 ] ] ] } } } }}) 
-> result B 

因此,有没有在这个意义上的MongoDB 2.4使用的MongoDB 2.6具有相同语义的任何方式,而不2dsphere索引的任何文件不要在“外部poylgon”查询中返回?

回答

4

2.6中的查询结果是正确的 - 2.4中的查询结果我认为我会调用不正确。从技术上讲,您的查询要求输入的文件与$geoWithin条件不符。 "k" : "B"文档与$geoWithin条件不匹配,所以应该由查询返回。您可以使用$exists下降而不p场结果:

db.x.find({ 
    "p" : { 
     "$exists" : true, 
     "$not" : { "$geoWithin" : { 
      "$geometry" : { 
       "type": "Polygon", 
       "coordinates" : [ [ [ 0, 0 ], [ 0, 4 ], [ 4, 0 ], [ 0, 0 ] ] ] 
      } 
    } } } 
}) 

还要注意:1)你的$not查询不实际使用地理指标,你可以用一个解释检查,和2)当使用2dsphere指数应该存储点作为GeoJSON的

{ 
    "k" : "A", 
    "p" : { 
     "type" : "Point", 
     "coordinates" : [1,1] 
    } 
} 

技术上它需要在MongoDB中> = 2.6,文档说应该是一个错误不使用GeoJSON的,但它似乎为我们工作。

+0

你的意思是在MongoDB 2.4中支持诸如'p:[1,1]'这样的“简单”坐标,但是MongoDB 2.6需要完整的GJSON,比如'p:{type:Point,coordinates:[1, 1]}'? – fgalan 2015-04-06 17:47:59

相关问题