文档菜单
文档首页
/
MongoDB 手册
/ / / /

$type

在本页

  • 定义
  • 兼容性
  • 语法
  • 行为
  • 示例
  • 按数组类型查询
  • 附加信息
$type

$type 根据字段的值是否是指定的类型实例来选择文档。当处理数据结构高度不规整且数据类型不可预测时,按数据类型查询非常有用。BSON 类型(s)。当处理数据结构高度不规整且数据类型不可预测时,按数据类型查询非常有用。

您可以使用 $type 对以下环境中的部署进行操作

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

单个$type表达式对应单个BSON类型,其语法如下

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

您可以指定BSON类型的数字或别名。

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

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

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

请参见通过多种数据类型进行查询以获取示例。

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

提示

另请参阅

$type 返回字段类型与传递给 $type 的BSON类型匹配的文档。

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

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

操作符 $type 除了接受与 BSON 类型对应的数字外,还接受字符串别名。 [1]

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

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

有关示例,请参阅 示例。

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

提示

另请参阅

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 包含地址和邮政编码,其中 zipCodestringintdoublelong

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