2015-04-01 41 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

相關問題