我已經運行了以下實驗,比較如何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