Mongoose & float values Mongoose & float values express express

Mongoose & float values


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.


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)],)


As of the current version of mongoose (v5.12.6), It supports Decimal128 which can be used for this.