APPENDIX · P1 · MONGODB · FOR SLIDE 5
← Back to Slide 5
P1: Validator and BSON Number Types
db.item_configs.findOne({ _id: "sword_dawn" },
{ name: 1, base_price: 1 })
db.item_configs.updateOne({ _id: "sword_dawn" },
{ $set: { base_price: 100 } })
db.item_configs.aggregate([{ $match: { _id: "sword_dawn" } },
{ $project: { t: { $type: "$base_price" } } }])
db.item_configs.updateOne({ _id: "sword_dawn" },
{ $set: { base_price: Double(100) } })
What the result should be
{ _id: 'sword_dawn', name: 'Blade of Dawn',
base_price: 100 }
matchedCount: 1, modifiedCount: 0
[ { _id: 'sword_dawn', t: 'int' } ]
Document failed validation // code 121
// price remains 100 (int)
Literal 100
Double(100)
Take name
In Mongosh, an integer that fits into an Int32 is stored as an int.
It makes sense, but the BSON double: validator with bsonType set to int rejects the record.
A valid document with a new _id and an already taken name will result in an E11000 error: the unique index was created explicitly.