2014-10-02 126 views
0

假設下面的架構數據添加到陣列中,我想節省一些GeoJSON的數據與貓鼬如何貓鼬模式

var simpleSchema = new Schema({ 
    properties:{ 
     name:String, 
     surname:String 
    }, 
    location : { 
     type : String, 
     coordinates : [ Number , Number ] 
    } 
}); 

這是我嘗試保存文檔

var a = new simple({properties:{name:"a", surname:"b"}, location:{type:"Point", coordinates:[1, 0]}}).save(function(err){...}); 

但是,我在數據庫中得到的是

ObjectId("542da9ab0882b41855ac3be0"), "properties" : { "name" : "a", "surname" : "b" }, "__v" : 0 } 

它看起來像整個位置標記和數據丟失。這是定義模式還是保存文檔的錯誤方式?

回答

2

在嵌入式對象中使用名爲type的字段時,需要使用對象來定義其類型,或者Mongoose認爲您正在定義對象本身的類型。

因此改變你的架構定義:

var simpleSchema = new Schema({ 
    properties:{ 
     name:String, 
     surname:String 
    }, 
    location : { 
     type : { type: String }, 
     coordinates : [ Number , Number ] 
    } 
}); 
+0

所以,這是一個貓鼬「的問題」,而不是一個MongoDB的? – Dimitris 2014-10-03 16:47:27

+1

@Dimitris對,這是一種貓鼬怪癖。 – JohnnyHK 2014-10-03 17:14:07