On this page
$type
在本页面
Definition
在版本 3.2 中更改。
{ field: { $type: <BSON type> } }
您可以为BSON type指定数字或别名
{ field: { $type: [ <BSON type1> , <BSON type2>, ... ] } }
上面的查询将匹配field
值为列出的任何类型的文档。数组中指定的类型可以是数字或字符串别名。
有关示例,请参见通过多种数据类型查询。
Available Types描述 BSON 类型及其对应的数字和字符串别名。
See also
如果您希望获取operator expression返回的BSON type而不是按文档的 BSON 类型过滤文档,请使用$type聚合运算符。
Behavior
$type返回field
的 BSON 类型与传递给$type的 BSON 类型匹配的文档。
在版本 3.6 中更改。
$type现在可以像处理其他 BSON 类型一样使用数组。以前的版本仅匹配field
包含嵌套数组的文档。
Available Types
在版本 3.2 中进行了更改:$type运算符除了对应于 BSON 类型的数字外,还接受 BSON 类型的字符串别名。以前的版本仅接受与 BSON 类型相对应的数字。
Type | Number | Alias | Notes |
---|---|---|---|
Double | 1 | "double" | |
String | 2 | "string" | |
Object | 3 | "object" | |
Array | 4 | "array" | |
Binary data | 5 | "binData" | |
Undefined | 6 | "undefined" | Deprecated. |
ObjectId | 7 | "objectId" | |
Boolean | 8 | "bool" | |
Date | 9 | "date" | |
Null | 10 | "null" | |
Regular Expression | 11 | "regex" | |
DBPointer | 12 | "dbPointer" | Deprecated. |
JavaScript | 13 | "javascript" | |
Symbol | 14 | "symbol" | Deprecated. |
JavaScript(带范围) | 15 | "javascriptWithScope" | |
32-bit integer | 16 | "int" | |
Timestamp | 17 | "timestamp" | |
64-bit integer | 18 | "long" | |
Decimal128 | 19 | "decimal" | 3.4 版的新功能。 |
Min key | -1 | "minKey" | |
Max key | 127 | "maxKey" |
$type支持number
别名,它将与以下BSON类型匹配:
double
32-bit integer
64-bit integer
decimal
See 按数据类型查询
MinKey 和 MaxKey
MinKey和MaxKey用于比较操作,主要用于内部使用。对于所有可能的BSON元素值,MinKey
将始终是最小值,而MaxKey
将始终是最大值。
用$type查询minKey
或maxKey
只会返回与特殊MinKey
或MaxKey
值匹配的字段。
假设data
集合具有两个文档MinKey
和MaxKey
:
{ "_id" : 1, x : { "$minKey" : 1 } }
{ "_id" : 2, y : { "$maxKey" : 1 } }
以下查询将返回带有_id: 1
的文档:
db.data.find( { x: { $type: "minKey" } } )
以下查询将返回带有_id: 2
的文档:
db.data.find( { y: { $type: "maxKey" } } )
Examples
按数据类型查询
addressBook
包含地址和邮政编码,其中zipCode
具有string
,int
,double
和long
值:
db.addressBook.insertMany(
[
{ "_id" : 1, address : "2030 Martian Way", zipCode : "90698345" },
{ "_id" : 2, address: "156 Lunar Place", zipCode : 43339374 },
{ "_id" : 3, address : "2324 Pluto Place", zipCode: NumberLong(3921412) },
{ "_id" : 4, address : "55 Saturn Ring" , zipCode : NumberInt(88602117) }
]
)
以下查询返回zipCode
是BSON类型string
的所有文档:
db.addressBook.find( { "zipCode" : { $type : 2 } } );
db.addressBook.find( { "zipCode" : { $type : "string" } } );
这些查询返回:
{ "_id" : 1, "address" : "2030 Martian Way", "zipCode" : "90698345" }
以下查询返回zipCode
是BSON类型double
的所有文档:
db.addressBook.find( { "zipCode" : { $type : 1 } } )
db.addressBook.find( { "zipCode" : { $type : "double" } } )
这些查询返回:
{ "_id" : 2, "address" : "156 Lunar Place", "zipCode" : 43339374 }
以下查询使用number
别名返回文档,其中zipCode
是BSON类型double
,int
或long
:
db.addressBook.find( { "zipCode" : { $type : "number" } } )
这些查询返回:
{ "_id" : 2, "address" : "156 Lunar Place", "zipCode" : 43339374 }
{ "_id" : 3, "address" : "2324 Pluto Place", "zipCode" : NumberLong(3921412) }
{ "_id" : 4, "address" : "55 Saturn Ring", "zipCode" : 88602117 }
按多种数据类型查询
grades
集合包含名称和平均值,其中classAverage
具有string
,int
和double
值:
db.grades.insertMany(
[
{ "_id" : 1, name : "Alice King" , classAverage : 87.333333333333333 },
{ "_id" : 2, name : "Bob Jenkins", classAverage : "83.52" },
{ "_id" : 3, name : "Cathy Hart", classAverage: "94.06" },
{ "_id" : 4, name : "Drew Williams" , classAverage : NumberInt("93") }
]
)
以下查询返回classAverage
是BSON类型string
或double
的所有文档。第一个查询使用数字别名,而第二个查询使用字符串别名。
db.grades.find( { "classAverage" : { $type : [ 2 , 1 ] } } );
db.grades.find( { "classAverage" : { $type : [ "string" , "double" ] } } );
这些查询返回以下文档:
{ "_id" : 1, "name" : "Alice King", "classAverage" : 87.33333333333333 }
{ "_id" : 2, "name" : "Bob Jenkins", "classAverage" : "83.52" }
{ "_id" : 3, "name" : "Cathy Hart", "classAverage" : "94.06" }
通过 MinKey 和 MaxKey 查询
restaurants
集合对任何不及格的成绩使用minKey
:
{
"_id": 1,
"address": {
"building": "230",
"coord": [ -73.996089, 40.675018 ],
"street": "Huntington St",
"zipcode": "11231"
},
"borough": "Brooklyn",
"cuisine": "Bakery",
"grades": [
{ "date": new Date(1393804800000), "grade": "C", "score": 15 },
{ "date": new Date(1378857600000), "grade": "C", "score": 16 },
{ "date": new Date(1358985600000), "grade": MinKey(), "score": 30 },
{ "date": new Date(1322006400000), "grade": "C", "score": 15 }
],
"name": "Dirty Dan's Donuts",
"restaurant_id": "30075445"
}
对于最高及格分数,则为maxKey
:
{
"_id": 2,
"address": {
"building": "1166",
"coord": [ -73.955184, 40.738589 ],
"street": "Manhattan Ave",
"zipcode": "11222"
},
"borough": "Brooklyn",
"cuisine": "Bakery",
"grades": [
{ "date": new Date(1393804800000), "grade": MaxKey(), "score": 2 },
{ "date": new Date(1378857600000), "grade": "B", "score": 6 },
{ "date": new Date(1358985600000), "grade": MaxKey(), "score": 3 },
{ "date": new Date(1322006400000), "grade": "B", "score": 5 }
],
"name": "Dainty Daisey's Donuts",
"restaurant_id": "30075449"
}
以下查询返回其grades.grade
字段包含minKey
的任何餐厅:
db.restaurants.find(
{ "grades.grade" : { $type : "minKey" } }
)
This returns
{
"_id" : 1,
"address" : {
"building" : "230",
"coord" : [ -73.996089, 40.675018 ],
"street" : "Huntington St",
"zipcode" : "11231"
},
"borough" : "Brooklyn",
"cuisine" : "Bakery",
"grades" : [
{ "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : "C", "score" : 15 },
{ "date" : ISODate("2013-09-11T00:00:00Z"), "grade" : "C", "score" : 16 },
{ "date" : ISODate("2013-01-24T00:00:00Z"), "grade" : { "$minKey" : 1 }, "score" : 30 },
{ "date" : ISODate("2011-11-23T00:00:00Z"), "grade" : "C", "score" : 15 }
],
"name" : "Dirty Dan's Donuts",
"restaurant_id" : "30075445"
}
以下查询返回其grades.grade
字段包含maxKey
的任何餐厅:
db.restaurants.find(
{ "grades.grade" : { $type : "maxKey" } }
)
This returns
{
"_id" : 2,
"address" : {
"building" : "1166",
"coord" : [ -73.955184, 40.738589 ],
"street" : "Manhattan Ave",
"zipcode" : "11222"
},
"borough" : "Brooklyn",
"cuisine" : "Bakery",
"grades" : [
{ "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : { "$maxKey" : 1 }, "score" : 2 },
{ "date" : ISODate("2013-09-11T00:00:00Z"), "grade" : "B", "score" : 6 },
{ "date" : ISODate("2013-01-24T00:00:00Z"), "grade" : { "$maxKey" : 1 }, "score" : 3 },
{ "date" : ISODate("2011-11-23T00:00:00Z"), "grade" : "B", "score" : 5 }
],
"name" : "Dainty Daisey's Donuts",
"restaurant_id" : "30075449"
}
按数组类型查询
名为SensorReading
的集合包含以下文档:
{
"_id": 1,
"readings": [
25,
23,
[ "Warn: High Temp!", 55 ],
[ "ERROR: SYSTEM SHUTDOWN!", 66 ]
]
},
{
"_id": 2,
"readings": [
25,
25,
24,
23
]
},
{
"_id": 3,
"readings": [
22,
24,
[]
]
},
{
"_id": 4,
"readings": []
},
{
"_id": 5,
"readings": 24
}
以下查询返回readings
字段为空或非空数组的任何文档。
db.SensorReading.find( { "readings" : { $type: "array" } } )
上面的查询返回以下文档:
{
"_id": 1,
"readings": [
25,
23,
[ "Warn: High Temp!", 55 ],
[ "ERROR: SYSTEM SHUTDOWN!", 66 ]
]
},
{
"_id": 2,
"readings": [
25,
25,
24,
23
]
},
{
"_id": 3,
"readings": [
22,
24,
[]
]
},
{
"_id": 4,
"readings": []
}
在具有_id : 1
,_id : 2
,_id : 3
和_id : 4
的文档中,readings
字段是一个数组。