I wanna make schema validation in mongoDB for double precision which modify value.
Like if I insert or update a double value 12.1111 it will save as 12.11 like we can do this in mysql DOUBLE(10, 2) when creating table.
I got answer like these but I wanna do it in schema if possible
$roundedDouble = round($double, 2);
$document = ['double' => $roundedDouble];
$collection->insertOne($document);
You can add validation with multipleOf
in your JSON Schema defintion
Keyword | Type | Definition | Behavior |
---|---|---|---|
multipleOf | numbers | number | Field must be a multiple of this value |
something like
{
"validator": {
"$jsonSchema": {
"bsonType": "object",
"properties": {
"score": {
"bsonType": "double",
"multipleOf": 0.01,
"description": "a double value with 2 points of precision"
}
}
}
}
}