Skip to content

Commit 727ca14

Browse files
temp
1 parent 0e5fd09 commit 727ca14

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/schematypes.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const schema = new Schema({
6969
mixed: Schema.Types.Mixed,
7070
_someId: Schema.Types.ObjectId,
7171
decimal: Schema.Types.Decimal128,
72+
double: Schema.Types.Double,
7273
array: [],
7374
ofString: [String],
7475
ofNumber: [Number],
@@ -648,6 +649,44 @@ const question = new Question({ answer: 42n });
648649
typeof question.answer; // 'bigint'
649650
```
650651

652+
### Double {#double}
653+
654+
Mongoose supports [64-bit IEEE 754-2008 floating point numbers](https://en.wikipedia.org/wiki/IEEE_754-2008_revision) as a SchemaType.
655+
Int32s are stored as [BSON type "double" in MongoDB](https://www.mongodb.com/docs/manual/reference/bson-types/).
656+
657+
```javascript
658+
const studentsSchema = new Schema({
659+
id: Int32
660+
});
661+
const Student = mongoose.model('Student', schema);
662+
663+
const student = new Temperature({ celsius: 1339 });
664+
typeof student.id; // 'number'
665+
```
666+
667+
There are several types of values that will be successfully cast to a Double.
668+
669+
```javascript
670+
new Temperature({ celsius: '1.2e12' }).celsius; // 15 as a Double
671+
new Temperature({ celsius: true }).celsius; // 1 as a Double
672+
new Temperature({ celsius: false }).celsius; // 0 as a Double
673+
new Temperature({ celsius: { valueOf: () => 83.0033 } }).celsius; // 83 as a Double
674+
new Temperature({ celsius: '' }).celsius; // null as a Double
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+
* strings that do not represent a numeric string, a NaN or a null-ish value
685+
* numbers in non-decimal or exponential format
686+
* objects that don't have a `valueOf()` function
687+
* an input that represents a value outside the bounds of a IEEE 754-2008 floating point
688+
689+
651690
## Getters {#getters}
652691

653692
Getters are like virtuals for paths defined in your schema. For example,

0 commit comments

Comments
 (0)