Docs 菜单
Docs 主页
/
MongoDB Manual
/ / / /

$type

在此页面上

  • 定义
  • 兼容性
  • 语法
  • 行为
  • 举例
  • 按数组类型查询
  • 更多信息
$type

$type选择field值为指定 BSON类型实例的文档。在处理数据类型不可预测的高度非结构化数据时,按数据类型查询非常有用。

可以使用 $type 查找托管在以下环境中的部署:

  • MongoDB Atlas :用于在云中部署 MongoDB 的完全托管服务

单个 BSON 类型的 $type 表达式具有以下语法:

{ field: { $type: <BSON type> } }

您可以指定 BSON 类型的编号或别名。

$type 表达式也可以接受 BSON 类型数组,语法如下:

{ field: { $type: [ <BSON type1> , <BSON type2>, ... ] } }

上述查询将匹配 field 值为所列任何类型的文档。数组中指定的类型可以是数字或字符串别名。

请参阅按多种数据类型查询,查看示例。

可用类型描述了 BSON 类型及其相应的数字和字符串别名。

提示

另请参阅:

$type 返回其中 field 的 BSON 类型与传递给 $type 的 BSON 类型相匹配的文档。

对于 field 为数组的文档,$type 返回的文档中至少有一个数组元素与传递给 $type 的类型匹配。

$type: "array" 的查询会返回字段本身为数组的文档。

$type 运算符除接受与 BSON 类型对应的编号外,还接受针对 BSON 类型的字符串别名。[ 1 ]

类型
数值
别名
注意
double
1
"double"
字符串
2
"string"
对象
3
"object"
阵列
4
"array"
二进制数据
5
"binData"
未定义
6
"undefined"
已弃用。
ObjectId
7
"objectId"
布尔
8
"bool"
Date
9
"date"
null
10
"null"
正则表达式
11
"regex"
数据库指针
12
"dbPointer"
已弃用。
JavaScript
13
"javascript"
符号
14
"symbol"
已弃用。
32 位整数
16
"int"
时间戳
17
"timestamp"
64 位整型
18
"long"
Decimal128
19
"decimal"
最小键
-1
"minKey"
Max key
127
"maxKey"

$type 支持 number 别名,该别名与以下 BSON 类型匹配:

有关示例,请参阅示例。

[1] 用户不能再使用查询过滤器 $type: 0 作为 $exists:false 的同义词。要查询 null 字段或缺失字段,请参阅查询 null 字段或缺失字段

提示

另请参阅:

MinKeyMaxKey用于比较操作,主要供内部使用。对于所有可能的BSON元素值, MinKey始终是最小值,而MaxKey始终是最大值。

使用 $type 查询 minKeymaxKey 只会返回与特殊 MinKeyMaxKey 值匹配的字段。

假设 data 集合有两个带 MinKeyMaxKey 的文档:

db.data.insertMany( [
{ _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" } } )

addressBook 包含地址和邮政编码,其中 zipCode 具有 stringintdoublelong值:

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) },
{ _id : 5, address : "104 Venus Drive", zipCode : ["834847278", "1893289032"] }
] )

以下查询将返回满足如下条件的所有文档:zipCodeBSON 类型 string 是包含指定类型元素的数组:

db.addressBook.find( { zipCode : { $type : 2 } } );
db.addressBook.find( { zipCode : { $type : "string" } } );

这些查询返回:

{ _id : 1, address : "2030 Martian Way", zipCode : "90698345" }
{ _id : 5, address : "104 Venus Drive", zipCode : [ "834847278", "1893289032" ] }

以下查询将返回满足如下条件的所有文档:zipCodeBSON 类型 double 是包含指定类型元素的数组:

db.addressBook.find( { zipCode : { $type : 1 } } );
db.addressBook.find( { zipCode : { $type : "double" } } );

这些查询返回:

{ _id : 2, address : "156 Lunar Place", zipCode : 43339374 }

以下查询使用number别名返回文档,其中zipCodeBSON 类型doubleintlong,或者是包含指定类型元素的数组:

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 具有 stringintdouble 值:

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") }
] )

以下查询将返回所有文档,其中 classAverageBSON 类型的 stringdouble再或是包含指定类型元素的数组。第一个查询使用数字别名,第二个查询则使用字符串别名。

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" }

restaurants 集合使用 minKey 来表示任何不及格的等级:

db.restaurants.insertOne( [
{
_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 适用于达到最高及格分数的任一成绩:

db.restaurants.insertOne( [
{
_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" } }
)

这将返回以下结果:

{
_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" } }
)

这将返回以下结果:

{
_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 的集合包含以下文档:

db.sensorReading.insertMany( [
{ _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 字段是一个数组。

后退

$exists

来年

评估查询