JavaScript には数値型が 1 つしかなく( Number
)、IEEE 754 浮動小数点数 (double) としてバイナリで表されます。
BSON 仕様では、これは double (タイプ 1) として表されるため、次のように検索できるはずです。
db.people.find({name: { $type: 1 }})
mongo
異なる BSONデータ型を挿入する場合は、いくつかのシェル ヘルパーがあります。
42 // Type 1: double (64-bit IEEE 754 floating point, 8 bytes)
NumberInt(42) // Type 16: int32 (32-bit signed integer, 4 bytes)
NumberLong(42) // Type 18: int64 (64-bit signed integer, 8 bytes)
たとえば、次のようになります。
db.people.insert({ name: 'default', num: 42 })
db.people.insert({ name: 'NumberLong', num: NumberLong(42) })
db.people.insert({ name: 'NumberInt', num: NumberInt(42) })
find()
複数の形式で表現できる数値 (つまり、32 ビット整数は double または int64 として表現することもできます) に対してを実行すると、さまざまな数値表現が一致します。
例えば:
db.people.find({num:42})
{
"_id" : ObjectId("50965aa3038d8c8e85fd3f45"),
"name" : "default",
"num" : 42
}
{
"_id" : ObjectId("50965aa3038d8c8e85fd3f46"),
"name" : "NumberLong",
"num" : NumberLong(42)
}
{
"_id" : ObjectId("50965aa3038d8c8e85fd3f47"),
"name" : "NumberInt",
"num" : 42
}
ただし、 で見つかった$type
場合、BSON 表現は異なります。
> db.people.find({num: { $type: 1 }})
{
"_id" : ObjectId("50965aa3038d8c8e85fd3f45"),
"name" : "default",
"num" : 42
}
> db.people.find({num: { $type: 16 }})
{
"_id" : ObjectId("50965aa3038d8c8e85fd3f47"),
"name" : "NumberInt",
"num" : 42
}
> db.people.find({num: { $type: 18 }})
{
"_id" : ObjectId("50965aa3038d8c8e85fd3f46"),
"name" : "NumberLong",
"num" : NumberLong(42)
}