$type (aggregation)
定義
$type
Returns a string that specifies the BSON Type of the argument.
$type
has the following operator expression syntax:{ $type: <expression> } The argument can be any valid 式.
以下も参照してください。
$isNumber
- 引数が数値であるかどうかを確認します。$type (Query)
- filters fields based on BSON type.
動作
$type
Unlike the $type
query operator, which matches array
elements based on their BSON type, the $type
aggregation operator does not examine array elements. Instead,
when passed an array as its argument, the $type
aggregation
operator returns the type of the argument, i.e. "array"
.
If the argument is a field that is missing in the input document,
$type
returns the string "missing"
.
The following table shows the $type
output for several
common types of expressions:
例 | 結果 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
注意
In the case of a literal array such as [ 1, 2, 3 ]
,
enclose the expression in an outer set
of array brackets to prevent MongoDB from parsing
[ 1, 2, 3 ]
as an
argument list
with three arguments (1, 2, 3
). Wrapping the array
[ 1, 2, 3 ]
in a $literal
expression
achieves the same result.
See operator expression syntax forms for more information.
利用可能なタイプ
タイプ | 番号 | エイリアス | ノート |
---|---|---|---|
Double | 1 | "double" | |
文字列 | 2 | "string" | |
オブジェクト | 3 | "object" | |
配列 | 4 | "array" | |
バイナリ データ | 5 | "binData" | |
未定義 | 6 | "undefined" | 非推奨。 |
ObjectId | 7 | "objectId" | |
ブール値 | 8 | "bool" | |
日付 | 9 | "date" | |
null | 10 | "null" | |
正規表現 | 11 | "regex" | |
DBポインタ | 12 | "dbPointer" | 非推奨。 |
JavaScript | 13 | "javascript" | |
シンボル | 14 | "symbol" | 非推奨。 |
32 ビット整数 | 16 | "int" | |
タイムスタンプ | 17 | "timestamp" | |
64 ビット整数 | 18 | "long" | |
Decimal128 | 19 | "decimal" | |
Min key | -1 | "minKey" | |
Max key | 127 | "maxKey" |
If the argument is a field that is missing in the input document,
$type
returns the string "missing"
.
例
This example uses a コレクション named coll
with
the following ドキュメント:
{ _id: 0, a : 8 } { _id: 1, a : [ 41.63, 88.19 ] } { _id: 2, a : { a : "apple", b : "banana", c: "carrot" } } { _id: 3, a : "caribou" } { _id: 4, a : NumberLong(71) } { _id: 5 }
The following aggregation operation uses the $type
operator to display the type of field a
for all documents
as part of the $project
stage.
db.coll.aggregate([{ $project: { a : { $type: "$a" } } }])
この操作では、以下を返します。
{ _id: 0, "a" : "double" } { _id: 1, "a" : "array" } { _id: 2, "a" : "object" } { _id: 3, "a" : "string" } { _id: 4, "a" : "long" } { _id: 5, "a" : "missing" }