2015-02-07 106 views
0

我使用mongolab作爲我的分貝主機。 我建立的地點,模式和保險指數「2dsphere」 文件例如:貓鼬不返回完整文檔

{ 
    "_id": { 
     "$oid": "54d6347ce4b04aad9bbdc8ac" 
    }, 
    "name": "Vacation", 
    "address": { 
     "city": "Ashkelon", 
     "street": "Afridat" 
    }, 
    "location": { 
     "type": "Point", 
     "coordinates": [ 
      34.552955, 
      31.671384 
     ] 
    } 
} 

當使用貓鼬查詢集合一切都很正常,但我不能檢索位置字段。 當使用mongo shell時,我得到完整的文檔(包含位置)。

// the query Im using: 
Mongoose: locations.find({ _id: ObjectId("54d63538e4b04aad9bbdc8b4") }) { fields: undefined } 

此查詢只返回以下域名:name,address和_id。

更新:

模式:

var locationSchema = mongoose.Schema({ 
    name: String, 
    address: {city: String, street: String}, 
    location: { 
     type: [Number], // [<longitude>, <latitude>] 
     index: '2dsphere'  // create the geospatial index 
    } 
}); 
+1

您可以發佈您的架構定義嗎? – 2015-02-07 19:36:26

+0

我更新了我的問題 – Tzelon 2015-02-07 19:58:51

回答

0

在模式中的location字段需要結構在您的文檔匹配。您當前的模式將其定義爲傳統的座標對,但您的文檔使用GeoJSON的Point,所以更改您的架構看起來像這個:分別

var locationSchema = mongoose.Schema({ 
    name: String, 
    address: {city: String, street: String}, 
    location: { 
     type: { type: String }, 
     coordinates: [Number] 
    } 
});  
locationSchema.index({location: '2dsphere'}); 

請注意,您需要定義索引location因爲它在模式中包含它自己的字段。

+0

謝謝你,先生。 返回閱讀有關傳統座標和新GeoJSON的更多信息。 – Tzelon 2015-02-08 18:28:12