我已经运行了以下实验,比较如何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”查询中返回?
你的意思是在MongoDB 2.4中支持诸如'p:[1,1]'这样的“简单”坐标,但是MongoDB 2.6需要完整的GJSON,比如'p:{type:Point,coordinates:[1, 1]}'? – fgalan 2015-04-06 17:47:59