4

I have a schema-less Model i.e. a blank schema as :

const NewSchema = new mongoose.Schema({

});

The reason I need this is, I am expecting unstructured and unexpected fields as of now during the development phase of my project.

var myModel = new Mongoose.model('myModel', NewSchema)

When it receives a float value like 0.1, it is stored as a "Double" in mongoDB. The issue is when it receives something like 1.0 or 0.0 which is saving by default as 1 and 0.

myModel.create({
  "videoLength" : 0.0
})

Result in database.

{
 "videoLength" : 0 (Int32)
}

Expectation is, for it to be saved as 1.0 and 0.0 as type Double. I know this is possible by specifying types in Schema but how to do this without specifying the fields with types explicitly in schema.

Piyush Upadhyay
  • 177
  • 1
  • 12
  • In JS you cannot have all trailing zeros after a decimal and maintain the datatype as number at the same time (Essentially 1.00000 is ~ 1). If the usecase is to maintain that precision -which can be achieved by converting it to string (a trade-off) and storing it as that. Using `.toFixed()` `toPrecision()`. – ambianBeing Jan 05 '20 at 15:55

0 Answers0