Mongoose & float value

My lat and lng numbers are converted to strings. My integers are still the correct Number data type. How to configure the model so that I can return my lat and lng as Float, not String?

I store latLng data in my db. Right now I have the Number data type for lat and lng set. When I check my db, I see this:

{ "_id" : ObjectId("563bd98a105249f325bb8a7e"), "lat" : 41.8126189999999980, "lng" : -87.8187850000000054, "created" : ISODate("2015-11-05T22:34:50.511Z"), "__v" : 0, "section" : 0, } 

But when I get my data using an expression, I get the following:

 { "_id": "563bd98a105249f325bb8a7e", "lat" : "41.8126189999999980", "lng" : "-87.8187850000000054", "__v": 0, "section" : 0, "created" : "2015-11-05T22:34:50.511Z", } 

My model:

 var WaypointSchema = new Schema({ lat: { type: Number }, lng: { type: Number }, section: { type: Number } created: { type: Date, default: Date.now } }); mongoose.model('Waypoint', WaypointSchema); 

Express controller:

 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); } }); }; 
+6
source share
3 answers

While mongoDB fully supports the float type, mongoose only supports the Number type, which is an integer. If you try to store the mongoDB floating-point number using the mongooses Number type, it will be converted to a string.

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

Your model after the changes will 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 this helps.

+10
source
 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)],) 
0
source

I get an error when trying to save any number using GraphiQL using the Double from require ('mongoose-double') (mongoose) schema type:

"A floating point number cannot represent a non-numeric value: {_bsontype: \" Double \ ", value: 3}",

  rating: { type: SchemaTypes.Double, min: 0.00, max: 5.00, }, 
0
source

Source: https://habr.com/ru/post/1235328/


All Articles