Node.js 猫鼬;浮动值
我的lat和lng编号正在转换为字符串。我的节整数仍然是正确的数字数据类型。如何设置模型,以便将lat和lng作为浮子而不是字符串取出 我正在数据库中存储latLng数据。现在,我已将lat和lng的数据类型设置为数字。当我查看数据库时,我看到:Node.js 猫鼬;浮动值,node.js,mongodb,express,meanjs,Node.js,Mongodb,Express,Meanjs,我的lat和lng编号正在转换为字符串。我的节整数仍然是正确的数字数据类型。如何设置模型,以便将lat和lng作为浮子而不是字符串取出 我正在数据库中存储latLng数据。现在,我已将lat和lng的数据类型设置为数字。当我查看数据库时,我看到: { "_id" : ObjectId("563bd98a105249f325bb8a7e"), "lat" : 41.8126189999999980, "lng" : -87.8187850000000054, "created" :
{
"_id" : ObjectId("563bd98a105249f325bb8a7e"),
"lat" : 41.8126189999999980,
"lng" : -87.8187850000000054,
"created" : ISODate("2015-11-05T22:34:50.511Z"),
"__v" : 0,
"section" : 0,
}
但是,当我使用express将数据取回时,我得到以下信息:
{
"_id": "563bd98a105249f325bb8a7e",
"lat" : "41.8126189999999980",
"lng" : "-87.8187850000000054",
"__v": 0,
"section" : 0,
"created" : "2015-11-05T22:34:50.511Z",
}
我的模型:
var WaypointSchema = new Schema({
lat: {
type: Number
},
lng: {
type: Number
},
section: {
type: Number
}
created: {
type: Date,
default: Date.now
}
});
mongoose.model('Waypoint', WaypointSchema);
快速控制器:
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);
}
});
};
虽然mongoDB完全支持
float
类型,但mongoose只支持Number
类型,即整数。如果您尝试使用mongooses类型的number
保存到mongoDB float number,它将转换为字符串
为了解决这个问题,您需要为mongoose加载一些插件,这将扩展其值类型。有一些插件最适合货币或日期,但在你的情况下,我会使用
更改后的模型将如下所示:
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);
希望能有所帮助。var mongoose=require('mongoose')var mongoose = require('mongoose');<br>
var Schema = mongoose.Schema;<br>
var Waypoint = new Schema({<br>
lat: {<br>
type: SchemaTypes.Double<br>
},<br>
lng: {<br>
type: SchemaTypes.Double<br>
},<br>
section: {<br>
type: Number<br>
}<br>
point: {<br>
type: [Number],<br>
index: '2d'<br>
},<br>
}, {<br>
timestamps: true<br>
})<br>
event.index({<br>
Point: '2dsphere'<br>
});<br>
module.exports = mongoose.model('Waypoint', Waypoint);<br>
waypoint.save(point: [parseFloat(values.latitude), parseFloat(values.longitude)],)
var Schema=mongoose.Schemavar航路点=新模式({
纬度:{
类型:SchemaTypes.Double
},
液化天然气:{
类型:SchemaTypes.Double
},
节:{
类型:编号
}
要点:{
类型:[编号],
索引:“2d”
},
},{
时间戳:真
})
事件索引({
点:“2dsphere”
});
module.exports=mongoose.model('航路点',航路点)
保存(点:[parseFloat(values.latitude),parseFloat(values.longitude)],)
从当前版本的mongoose(v5.12.6)开始,它支持用于此目的的选项。您可以编辑您的问题以显示创建该输出的代码吗?很抱歉,对于我的情况,如果我试图保存浮点值,那么它将仅存储为浮点值。