Mongoose & float values

随声附和 提交于 2019-12-05 04:21:48

While the mongoDB fully supports float type, the mongoose supports only type of Number which is integer. If you try to save to mongoDB float number using mongooses type of Number it will be converted to string.

To sort this out, you will need to load some plugin for mongoose which will extend its value types. There are some plugins which work best with currencies or dates, but in your case I would use https://www.npmjs.com/package/mongoose-double.

Your model after changes would look something like this:

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);

Hope it helps.

Javascriptsoldier
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)],)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!