You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mongoose supports [64-bit IEEE 754-2008 floating point numbers](https://en.wikipedia.org/wiki/IEEE_754-2008_revision) as a SchemaType.
657
+
Int32s are stored as [BSON type "double" in MongoDB](https://www.mongodb.com/docs/manual/reference/bson-types/).
658
+
659
+
```javascript
660
+
conststudentsSchema=newSchema({
661
+
id: Int32
662
+
});
663
+
constStudent=mongoose.model('Student', schema);
664
+
665
+
conststudent=newTemperature({ celsius:1339 });
666
+
typeofstudent.id; // 'number'
667
+
```
668
+
669
+
There are several types of values that will be successfully cast to a Double.
670
+
671
+
```javascript
672
+
newTemperature({ celsius:'1.2e12' }).celsius; // 15 as a Double
673
+
newTemperature({ celsius:true }).celsius; // 1 as a Double
674
+
newTemperature({ celsius:false }).celsius; // 0 as a Double
675
+
newTemperature({ celsius: { valueOf: () =>83.0033 } }).celsius; // 83 as a Double
676
+
newTemperature({ celsius:'' }).celsius; // null as a Double
677
+
```
678
+
679
+
The following inputs will result will all result in a [CastError](validation.html#cast-errors) once validated, meaning that it will not throw on initialization, only when validated:
680
+
681
+
* strings that do not represent a numeric string, a NaN or a null-ish value
682
+
* objects that don't have a `valueOf()` function
683
+
* an input that represents a value outside the bounds of a IEEE 754-2008 floating point
684
+
685
+
### Int32 {#int32}
686
+
687
+
Mongoose supports 32-bit integers as a SchemaType.
688
+
Int32s are stored as [32-bit integers in MongoDB (BSON type "int")](https://www.mongodb.com/docs/manual/reference/bson-types/).
689
+
690
+
```javascript
691
+
conststudentsSchema=newSchema({
692
+
id: Int32
693
+
});
694
+
constStudent=mongoose.model('Student', schema);
695
+
696
+
conststudent=newTemperature({ celsius:1339 });
697
+
typeofstudent.id; // 'number'
698
+
```
699
+
700
+
There are several types of values that will be successfully cast to a Number.
701
+
702
+
```javascript
703
+
newStudent({ id:'15' }).id; // 15 as a Int32
704
+
newStudent({ id:true }).id; // 1 as a Int32
705
+
newStudent({ id:false }).id; // 0 as a Int32
706
+
newStudent({ id: { valueOf: () =>83 } }).id; // 83 as a Int32
707
+
newStudent({ id:'' }).id; // null as a Int32
708
+
```
709
+
710
+
If you pass an object with a `valueOf()` function that returns a Number, Mongoose will
711
+
call it and assign the returned value to the path.
712
+
713
+
The values `null` and `undefined` are not cast.
714
+
715
+
The following inputs will result will all result in a [CastError](validation.html#cast-errors) once validated, meaning that it will not throw on initialization, only when validated:
716
+
717
+
* NaN
718
+
* strings that cast to NaN
719
+
* objects that don't have a `valueOf()` function
720
+
* a decimal that must be rounded to be an integer
721
+
* an input that represents a value outside the bounds of an 32-bit integer
722
+
650
723
## Getters {#getters}
651
724
652
725
Getters are like virtuals for paths defined in your schema. For example,
* Given a value, cast it to a IEEE 754-2008 floating point, or throw an `Error` if the value
9
+
* cannot be casted. `null`, `undefined`, and `NaN` are considered valid inputs.
10
+
*
11
+
* @param {Any} value
12
+
* @return {Number}
13
+
* @throws {Error} if `value` does not represent a IEEE 754-2008 floating point. If casting from a string, see [BSON Double.fromString API documentation](https://mongodb.github.io/node-mongodb-native/Next/classes/BSON.Double.html#fromString)
14
+
* @api private
15
+
*/
16
+
17
+
module.exports=functioncastDouble(val){
18
+
if(val==null||val===''){
19
+
returnnull;
20
+
}
21
+
22
+
letcoercedVal;
23
+
if(isBsonType(val,'Long')){
24
+
coercedVal=val.toNumber();
25
+
}elseif(typeofval==='string'){
26
+
try{
27
+
coercedVal=BSON.Double.fromString(val);
28
+
returncoercedVal;
29
+
}catch{
30
+
assert.ok(false);
31
+
}
32
+
}elseif(typeofval==='object'){
33
+
consttempVal=val.valueOf()??val.toString();
34
+
// ex: { a: 'im an object, valueOf: () => 'helloworld' } // throw an error
0 commit comments