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 32-bit integers as a SchemaType.
655
+
Int32s are stored as [32-bit integers in MongoDB (BSON type "int")](https://www.mongodb.com/docs/manual/reference/bson-types/).
656
+
657
+
```javascript
658
+
conststudentsSchema=newSchema({
659
+
id: Int32
660
+
});
661
+
constStudent=mongoose.model('Student', schema);
662
+
663
+
conststudent=newStudent({ id:1339 });
664
+
typeofstudent.id; // 'number'
665
+
```
666
+
667
+
There are several types of values that will be successfully cast to a Number.
668
+
669
+
```javascript
670
+
newStudent({ id:'15' }).id; // 15 as a Int32
671
+
newStudent({ id:true }).id; // 1 as a Int32
672
+
newStudent({ id:false }).id; // 0 as a Int32
673
+
newStudent({ id: { valueOf: () =>83 } }).id; // 83 as a Int32
674
+
newStudent({ id:'' }).id; // null as a Int32
675
+
```
676
+
677
+
If you pass an object with a `valueOf()` function that returns a Number, Mongoose will
678
+
call it and assign the returned value to the path.
679
+
680
+
The values `null` and `undefined` are not cast.
681
+
682
+
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:
683
+
684
+
* NaN
685
+
* strings that cast to NaN
686
+
* objects that don't have a `valueOf()` function
687
+
* a decimal that must be rounded to be an integer
688
+
* an input that represents a value outside the bounds of an 32-bit integer
689
+
650
690
## Getters {#getters}
651
691
652
692
Getters are like virtuals for paths defined in your schema. For example,
0 commit comments