2016-07-05 76 views
0

我們正在嘗試將位置日誌保存到集合中。貓鼬架構的定義如下 -在使用Mongoose在MongoDB中保存文檔時如何保留字段的順序?

{ user: { type: String, required: true }, location: { longitude: {type: Number}, latitude: type: { type: Number}}

我們保存定位日誌通過我們的代碼(用法如下) -

Model.findOneAndUpdate({user: 1}, 
    {location:{longitude: 9.0, latitude: 10.0}}, 
    function(err) {...}); 

上詢問過3T MongoChef數據庫中,我們觀察到,地點對象保存的順序不一致,導致地理位置的索引編制錯誤。即使兩個用戶具有相同的位置,我們也只能得到鍵的排序格式爲(latitude, longitude)的結果。

回答

0

不確定這不是一個錯誤。但你可以在presave鉤子處重新組裝位置:

schema.pre('save', function(next) { 
    this.location = { 
    latitude: this.location.latitude, 
    longitude: this.location.longitude, 
    }; 

    next(); 
}); 

希望這會有所幫助。

相關問題