Decimal / Float in mongoose for node.js

I start my first test app on node.js / mongoDB / mongoose, this is a very simple app that aims to crate record in DB and retrieve them.

I create a model like:

var Car = new Schema({
    brand : String,
    speed  : Number,
    date  :  { type: Date, default: }

This is working fine, except that I would like to be able to provide a float value for speed instead of the integer one. I gave a try to Decimal and Float but none of them are working. I did not find in the documentation either.

Any idea ?

I've searched a bit and found this article stating that for storing float values you must use Number type. You can store any float value in speed field.

A Node.js Perspective on MongoDB 3.4: Decimal Type, What's Wrong With Binary Floating Points? Here's a quick example of inserting a document into MongoDB with a property x with initial value 0.1,  I am not familiar with mongoose and node.js at all, but I’ve searched a bit and found that for storing float values you must use Number type. You can store any float value in speed field. Article. Hope this help.

Yes you can use the Decimal128 type.

mongoose-float, This library can solve one well known problem with JavaScript Number arithmetic imprecise, when, for example, 3.3 * 3 becomes  Decimal in Node.js JavaScript has no native support for decimal floating points, so working with the decimal type has some troublesome edge cases. Here's a rudimentary example of using the decimal type in Node.js. Keep in mind that the decimal type is new in version 2.2.0 of the MongoDB Node.js driver, so make sure you use mongodb >= 2.2.0.

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

Your model after changes would look something like this:

var mongoose = require('mongoose')

var SchemaTypes = mongoose.Schema.Types;
var Car = new Schema({
    brand: { 
        type: String 
    speed: {
        type: SchemaTypes.Double
    date: {
        type: Date, 

Hope it helps.

mongoose.Decimal128 JavaScript and Node.js code examples , Best JavaScript code snippets using mongoose. forEach((key, index) => { // Parse to float number. returned[key] = parseFloat(returned[key].toString()); }); buildValidator(validator, isRequired), message: '{VALUE} is not a Decimal value',​  Informations Node.js version: 10.12.0 npm version: 5.6.0 Strapi version: 3.0.0-alpha.14.4.0 Database: MongoDB Operating system: Windows 10 What is the current behavior? When creating a number field, if "Float" or "Decimal" are selected f

question: does Mongoose support decimals and integers (MongoDB , Mongoose 5.0.8 MongoDB 3.6 in the docs I can still only find Number as decimals and integers (MongoDB NumberDecimal, NumberLong, NumberInt) It's based on older versions of mongo*/node, but I don't believe any of  Extract Decimal from Decimal128 with Mongoose - MongoDB javascript node.js mongodb mongoose. is that the values are meant to be too large and precise for a float.

Automattic/mongoose, Currently, I am using mongoose-float instead of Decimal. Start the DB service with normal "mongod" and the application with "node app.js"  Using the Decimal BSON Type which is a decimal-based floating-point format capable of providing exact precision. Available in MongoDB version 3.4 and later. Using a Scale Factor to convert the monetary value to a 64-bit integer (long BSON type) by multiplying by a power of 10 scale factor.

🥊 🧜 Decimal / mongoose for node.js, I am not at all familiar with mongoose and node.js, but I searched a bit and found that you must use the Number type to store the float values. You can save any  @anwar you can't reliably round a float (arguably you can round a float at all) - you'll get something like 0.0500 or even 0.0500000000001. See – jmc Jan 12 '18 at 0:08 2

  • var car = new Car({brand: "", speed: 0.5});
  • thanks a lot . But, in this article, it seems it's more the other way round, ie to force a float into an integer, right ? Sorry, I maybe misanderstood the thing.
  • @Luc: Exactly, in article about forcing float into an integer. But it's mean that you can store float value in field with type Number. So Number type should be used for both types: integer and float.
  • @Bugain13, thanks a lot, you'r right. There is something strange I should have done because it was not working at first place. Thanks a lot for your help !!!!
  • You should be warned: When using number and aggregations like summations with Mongoose you will get Ulps problems. (insertion of a 1 after dozens of Zeros behind the point)
  • No it won't. You can store numbers like 25.50 as a number and it won't be stored as a string