title: MongoError: Document failed validation - How to insert both float and int into the same field - which is marked as double?
body: <p>I'm having problems with inserting into mongoDB because <strong>good objects don't pass the mongoDB validator</strong>. </p>
  
  <p>To make it worse, the error is a generic: <code>Document failed validation</code> which in a big multi-nested object can make things confusing regarding where exactly this validation fails.</p>
  
  <pre><code>myValidatorIs =
    { validator:
        { $jsonSchema :
            { bsonType: "object"
            , required: [ "price" ]
            , properties:
                { price:
                    { bsonType: "double" // price needs to be a double, tried with decimal also.
                    , description: "must be a double/float and is required"
                    }
                }
            }
        }
    , validationAction: "error"
    , validationLevel: "strict"
    };
  
  db.collection("collection").insertOne({ price : 4.5 }); // this works 
  db.collection("collection").insertOne({ price : 4.0 }); // this doesn't - see error below
  </code></pre>
  
  <blockquote>
    <p>ERROR:  UnhandledPromiseRejectionWarning: MongoError: Document failed validation</p>
  </blockquote>
  
  <p>My app needs something different but i simplified it here by using <code>price</code>. </p>
  
  <p>Now after lots of trial and error i figured out what actually happens. Was not as clear as above.</p>
  
  <p>Basically in javascript the <code>4.0</code> (<code>float</code>) is implicitly transformed to <code>4</code> (<code>integer</code>)
  And this <code>integer</code> fails the validation since is not a <code>float</code>. Which is super wired. Since this data comes from outside, i can't control if is <code>float</code> or <code>integer</code>. Javascript only knows about <code>number</code>.</p>
  
  <p>Is this indeed the problem? I mean i tried lots of different things and <strong>i can't see any other reason</strong> other then this <strong>implicit type conversion</strong>. </p>
  
  <p>Because in the validator - if i set <code>bsonType : "int"</code> and i give it <code>{price: 1}</code> or <code>{price: 4.0}</code> then the insertion works with no errors.</p>
  
  <p>How to deal with this type of issue? How to insert <code>{price: 4.0}</code>?</p>
  
  <p>Also what settings should i include to make that <code>description</code> field i set to appear in the error message? After all what is the purpose of <code>properties.price.description</code> if not to create better error messages ?</p>