// insert a document into a new table; number is interpreted as a double
|
db.example.insert({"foo": 1})
|
//--> WriteResult({ "nInserted" : 1 })
|
|
// good so far
|
db.example.find()
|
//--> { "_id" : ObjectId("56b4c6dcbfa50a533004158a"), "foo" : 1 }
|
|
// confirm the field is of type double
|
// ref: https://docs.mongodb.org/manual/reference/operator/query/type
|
db.example.count({"foo": {"$type": 1}})
|
//--> 1
|
|
// attempt to update field from double to integer; fails
|
db.example.update({"foo": {"$type": 1}}, {"$set": {"foo": NumberInt(1) }})
|
//--> WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
|
|
// doc still has a double
|
db.example.count({"foo": {"$type": 1}})
|
//--> 1
|
|
// update field from double to integer of a different value; succeeds
|
db.example.update({"foo": {"$type": 1}}, {"$set": {"foo": NumberInt(2) }})
|
// WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
|
|
// doc no longer has a double
|
db.example.count({"foo": {"$type": 1}})
|
//--> 0
|
|
// confirm doc now has an int
|
db.example.count({"foo": {"$type": 16}})
|
//--> 1
|