Jus*_*ung 7 mongodb node.js express meanjs
我的lat&lng数字正在转换为字符串.我的部分整数仍然是Number的正确数据类型.如何设置模型以便我可以将我的lat&lng作为Float而不是String返回?
我在我的数据库中存储latLng数据.现在我将我的数据类型设置为Lat和lng的数字.当我检查我的数据库时,我看到了这个:
{
"_id" : ObjectId("563bd98a105249f325bb8a7e"),
"lat" : 41.8126189999999980,
"lng" : -87.8187850000000054,
"created" : ISODate("2015-11-05T22:34:50.511Z"),
"__v" : 0,
"section" : 0,
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用快递我的数据时,我得到了这个:
{
"_id": "563bd98a105249f325bb8a7e",
"lat" : "41.8126189999999980",
"lng" : "-87.8187850000000054",
"__v": 0,
"section" : 0,
"created" : "2015-11-05T22:34:50.511Z",
}
Run Code Online (Sandbox Code Playgroud)
我的模特:
var WaypointSchema = new Schema({
lat: {
type: Number
},
lng: {
type: Number
},
section: {
type: Number
}
created: {
type: Date,
default: Date.now
}
});
mongoose.model('Waypoint', WaypointSchema);
Run Code Online (Sandbox Code Playgroud)
快速控制器:
exports.list = function(req, res) {
Waypoint.find().sort('-created').populate('user', 'displayName').exec(function(err, waypoints) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(waypoints);
}
});
};
Run Code Online (Sandbox Code Playgroud)
sha*_*nSK 11
虽然mongoDB完全支持float类型,但是mongoose 仅支持Number整数类型.如果你尝试使用mongooses类型保存到mongoDB浮点数Number,它将被转换为字符串.
要对此进行排序,您需要为mongoose加载一些插件,这将扩展其值类型.有一些插件最适合使用货币或日期,但在您的情况下,我会使用https://www.npmjs.com/package/mongoose-double.
更改后的模型看起来像这样:
var mongoose = require('mongoose')
require('mongoose-double')(mongoose);
var SchemaTypes = mongoose.Schema.Types;
var WaypointSchema = new Schema({
lat: {
type: SchemaTypes.Double
},
lng: {
type: SchemaTypes.Double
},
section: {
type: Number
}
created: {
type: Date,
default: Date.now
}
});
mongoose.model('Waypoint', WaypointSchema);
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
| 归档时间: |
|
| 查看次数: |
11235 次 |
| 最近记录: |