On this page本页内容
$type¶$type selects documents where the value of the 选择field is an instance of the specified BSON type(s). field值为指定BSON类型实例的文档。Querying by data type is useful when dealing with highly unstructured data where data types are not predictable.在处理数据类型不可预测的高度非结构化数据时,按数据类型查询非常有用。
A 单个BSON类型的$type expression for a single BSON type has the following syntax:$type表达式语法如下所示:
Changed in version 3.2.在版本3.2中更改。
You can specify either the number or alias for the BSON type您可以指定BSON类型的编号或别名
The $type expression can also accept an array of BSON types and has the following syntax:$type表达式还接受BSON类型的数组,语法如下所示:
The above query will match documents where the 上述查询将匹配field value is any of the listed types. field值为列出的任何类型的文档。The types specified in the array can be either numeric or string aliases.数组中指定的类型可以是数字别名或字符串别名。
See Querying by Multiple Data Type for an example.有关示例,请参阅按多个数据类型查询。
Available Types describes the BSON types and their corresponding numeric and string aliases.可用类型描述BSON类型及其相应的数字和字符串别名。
See also参阅
$isNumber - $type (Aggregation) - $type returns documents where the BSON type of the 返回field matches the BSON type passed to $type.field的BSON类型与传递给$type的BSON类型匹配的文档。
For documents where 对于field is an array, $type returns documents in which at least one array element matches a type passed to $type.field为数组的文档,$type返回其中至少有一个数组元素与传递给$type的类型匹配的文档。
With MongoDB 3.6 and later, querying for 对于MongoDB 3.6及更高版本,查询$type: "array" returns documents where the field itself is an array. $type: "array"将返回字段本身为数组的文档。Prior to MongoDB 3.6, 在MongoDB 3.6之前,$type: "array" returned documents where the field is an array containing at least one element of type array. $type: "array"返回的文档中,字段是一个数组,其中至少包含一个array类型的元素。For example, given the following documents:例如,给定以下文档:
With MongoDB 3.6 and later, the query 对于MongoDB 3.6及更高版本,查询find( {"data" : { $type : "array" } } ) returns both documents. find( {"data" : { $type : "array" } } )将返回这两个文档。Prior to MongoDB 3.6, the query returns only the first document.在MongoDB 3.6之前,查询只返回第一个文档。
Starting in MongoDB 3.2, 从MongoDB 3.2开始,$type operator accepts string aliases for the BSON types in addition to the numbers corresponding to the BSON types. $type运算符除了接受与BSON类型对应的数字外,还接受BSON类型的字符串别名。Previous versions only accepted the numbers corresponding to the BSON type. 以前的版本只接受与BSON类型对应的数字。[1]
| Alias | |||
|---|---|---|---|
| Double | 1 | “double” | |
| String | 2 | “string” | |
| Object | 3 | “object” | |
| Array | 4 | “array” | |
| Binary data | 5 | “binData” | |
| Undefined | 6 | “undefined” | |
| ObjectId | 7 | “objectId” | |
| Boolean | 8 | “bool” | |
| Date | 9 | “date” | |
| Null | 10 | “null” | |
| Regular Expression | 11 | “regex” | |
| DBPointer | 12 | “dbPointer” | |
| JavaScript | 13 | “javascript” | |
| Symbol | 14 | “symbol” | |
| JavaScript code with scope | 15 | “javascriptWithScope” | Deprecated in MongoDB 4.4. |
| 32-bit integer | 16 | “int” | |
| Timestamp | 17 | “timestamp” | |
| 64-bit integer | 18 | “long” | |
| Decimal128 | 19 | “decimal” | New in version 3.4. |
| Min key | -1 | “minKey” | |
| Max key | 127 | “maxKey” |
$type supports the number alias, which will match against the following BSON types:$type支持number别名,该别名将与以下BSON类型匹配:
For examples, see Examples.有关示例,请参阅示例。
| [1] | $type: 0 as a synonym for $exists:false. $type:0用作$exists:false的同义词。 |
See also参阅
$isNumber New in MongoDB 4.4MongoDB 4.4中的新功能
MinKey and MaxKey are used in comparison operations and exist primarily for internal use. MinKey和MaxKey用于比较操作,主要用于内部使用。For all possible BSON element values, 对于所有可能的BSON元素值,MinKey will always be the smallest value while MaxKey will always be the greatest value.MinKey始终是最小值,而MaxKey始终是最大值。
Querying for 使用minKey or maxKey with $type will only return fields that match the special MinKey or MaxKey values.$type查询minKey或maxKey将只返回与特殊MinKey或MaxKey值匹配的字段。
Suppose that the 假设data collection has two documents with MinKey and MaxKey:data集合有两个具有MinKey和MaxKey的文档:
The following query will return the document with 以下查询将返回_id: 1:_id: 1的文档:
The following query will return the document with 以下查询将返回_id: 2:_id: 2的文档:
The addressBook contains addresses and zipcodes, where zipCode has string, int, double, and long values:addressBook包含地址和邮政编码,其中zipCode具有string、int、double和long值:
The following queries return all documents where 以下查询返回zipCode is the BSON type string or is an array containing an element of the specified type:zipCode为BSON类型string或包含指定类型元素的数组的所有文档:
These queries return:这些查询返回:
The following queries return all documents where 以下查询返回zipCode is the BSON type double or is an array containing an element of the specified type:zipCode为BSON类型double或包含指定类型元素的数组的所有文档:
These queries return:这些查询返回:
The following query uses the 以下查询使用number alias to return documents where zipCode is the BSON type double, int, or long or is an array containing an element of the specified types:number别名返回zipCode为BSON类型double、int或long或包含指定类型元素的数组的文档:
These queries return:这些查询返回:
The grades collection contains names and averages, where classAverage has string, int, and double values:grades集合包含名称和平均值,其中classAverage具有string、int和double值:
The following queries return all documents where 以下查询返回classAverage is the BSON type string or double or is an array containing an element of the specified types. classAverage为BSON类型string或double或包含指定类型元素的数组的所有文档。The first query uses numeric aliases while the second query uses string aliases.第一个查询使用数字别名,而第二个查询使用字符串别名。
These queries return the following documents:这些查询返回以下文档:
The restaurants collection uses minKey for any grade that is a failing grade:restaurants集合对任何不合格的等级使用minKey:
And 以及最高通过等级的任何等级的maxKey for any grade that is the highest passing grade:MaxKey:
The following query returns any restaurant whose 以下查询返回其grades.grade field contains minKey or is an array containing an element of the specified type:grades.grade字段包含minKey或是包含指定类型元素的数组的任何餐厅:
This returns它返回
The following query returns any restaurant whose 以下查询返回其grades.grade field contains maxKey or is an array containing an element of the specified type:grades.grade字段包含maxKey或是包含指定类型元素的数组的任何餐厅:
This returns它返回
A collection named 名为SensorReading contains the following documents:SensorReading的集合包含以下文档:
The following query returns any document in which the 下面的查询返回任何文档,其中readings field is an array, empty or non-empty.readings字段为数组,为空或非空。
The above query returns the following documents:上述查询返回以下文档:
In the documents with 在具有_id : 1, _id : 2, _id : 3, and _id : 4, the readings field is an array._id:1、_id:2、_id:3和_id:4的文档中,readings字段是一个数组。