Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store floating point values in Meteor (JS) using Collections2?

I have a Number data type in the schema (simple-schema) but am unable to store floating point numbers in it when using collections2:

Schema.Coordinates = new SimpleSchema({ lng: { type: Number, min: -180.0, max: 180.0 }, lat: { type: Number, min: -90.0, max: 90.0 } });

When I try to insert anything other than an integer (anything with xxxx.0) I get a validation error:

W20150222-20:24:23.523(-8)? (STDERR) Error: Lng must be an integer

like image 307
Jason Leach Avatar asked Dec 12 '25 13:12

Jason Leach


2 Answers

As has already been stated, setting decimal to true will allow for floating point numbers.

I just wanted to make another suggestion. Since you're trying to store log/lat this would be a better schema:

loc:
   type: Object
   index: '2dsphere'
   label: "Location"

"loc.type": 
   type: String
   allowedValues: [ "Point" ]
   label: "Start location type"

"loc.coordinates":
   type: [Number]
   minCount: 2
   maxCount: 2
   decimal: true

That allows you to store the coordinates in GeoJSON format so that you can then use Mongo's spacial operators (such as $near) on the server.

like image 52
tarmes Avatar answered Dec 14 '25 14:12

tarmes


You can set decimal to true (docs). I guess this is a bit like optional else like the other answer.

Schema.Coordinates = new SimpleSchema({
    lng: {
        type: Number,
        min: -180.0,
        max: 180.0,
        decimal:true,
    },
    lat: {
        type: Number,
        min: -90.0,
        max: 90.0,
        decimal: true,
    }
});
like image 41
Tarang Avatar answered Dec 14 '25 13:12

Tarang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!