定义
兼容性
可以使用 $type 查找托管在以下环境中的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
{ field: { $type: <BSON type> } }
您可以指定 BSON 类型的编号或别名。
$type 表达式也可以接受 BSON 类型数组,语法如下:
{ field: { $type: [ <BSON type1> , <BSON type2>, ... ] } }
上述查询将匹配 field 值为所列任何类型的文档。数组中指定的类型可以是数字或字符串别名。
有关示例,请参阅按多种数据类型进行查询。
可用类型描述了 BSON 类型及其相应的数字和字符串别名。
提示
$isNumber— 检查参数是否为数字。$type (Aggregation)— 返回参数的 BSON 类型。
行为
$type 返回其中 field 的 BSON 类型与传递给 $type 的 BSON 类型相匹配的文档。
数组
对于 field 为数组的文档,$type 返回的文档中至少有一个数组元素与传递给 $type 的类型匹配。
对 $type: "array" 的查询会返回字段本身为数组的文档。
可用类型
$type操作符除了接受与BSON types对应的数字之外,还接受BSON 类型的字符串别名。
$type 支持 number 别名,该别名与以下 BSON 类型匹配:
有关示例,请参阅示例。
Minkey 和 MaxKey
MinKey 和 MaxKey 用于比较操作,主要供内部使用。对于所有可能的BSON元素值,MinKey 始终是最小值,MaxKey 始终是最大值。
使用 $type 查询 minKey 或 maxKey 只会返回与特殊 MinKey 或 MaxKey 值匹配的字段。
示例,以下 data集合包含这些带有 MinKey 和 MaxKey 的文档:
db.data.insertMany( [ { _id: 1, x: MinKey() }, { _id: 2, y: MaxKey() } ] )
以下查询返回带有 _id: 1 的文档:
db.data.find( { x: { $type: "minKey" } } )
[ { _id: 1, x: MinKey() } ]
以下查询返回带有 _id: 2 的文档:
db.data.find( { y: { $type: "maxKey" } } )
[ { _id: 2, y: MaxKey() } ]
示例
按数据类型进行查询
本页上的示例使用sample_mflix示例数据集中的数据。有关如何将此数据集加载到自管理MongoDB 部署中的详细信息,请参阅加载示例数据集。如果对示例数据库进行了任何修改,则可能需要删除并重新创建数据库才能运行本页上的示例。
movies集合将 IMDb 评级存储在 imdb.rating字段中。大多数文档将 imdb.rating存储为 double,但某些文档将其存储为空 string ("")。
以下查询返回来自 2013 的文档,其中 imdb.rating 是BSON类型 string。
此查询指定带有BSON类型编号的类型。
db.movies.find( { "imdb.rating": { $type: 2 }, year: 2013 }, { _id: 0, title: 1, year: 1, "imdb.rating": 1 } )
[ { title: 'Coming to Terms', year: 2013, imdb: { rating: '' } }, { title: 'Absent Minded', year: 2013, imdb: { rating: '' } } ]
此查询指定具有BSON类型别名的类型:
db.movies.find( { "imdb.rating": { $type: "string" }, year: 2013 }, { _id: 0, title: 1, year: 1, "imdb.rating": 1 } )
[ { title: 'Coming to Terms', year: 2013, imdb: { rating: '' } }, { title: 'Absent Minded', year: 2013, imdb: { rating: '' } } ]
以下查询使用 number 别名返回文档,其中 imdb.rating 为BSON类型 double、int 或 long:
db.movies.find( { "imdb.rating": { $type: "number" }, runtime: { $gt: 1000 } }, { _id: 0, title: 1, runtime: 1, "imdb.rating": 1 } )
[ { runtime: 1256, title: 'Centennial', imdb: { rating: 8.5 } }, { runtime: 1140, title: 'Baseball', imdb: { rating: 9.1 } } ]
按多种数据类型查询
以下查询返回 imdb.rating 为BSON类型 string 或 double 的文档。第一个查询使用数字别名,第二个查询使用字符串别名。
db.movies.find( { "imdb.rating": { $type: [ 2, 1 ] }, runtime: { $gt: 1000 } }, { _id: 0, title: 1, runtime: 1, "imdb.rating": 1 } )
[ { runtime: 1256, title: 'Centennial', imdb: { rating: 8.5 } }, { runtime: 1140, title: 'Baseball', imdb: { rating: 9.1 } } ]
db.movies.find( { "imdb.rating": { $type: [ "string", "double" ] }, runtime: { $gt: 1000 } }, { _id: 0, title: 1, runtime: 1, "imdb.rating": 1 } )
[ { runtime: 1256, title: 'Centennial', imdb: { rating: 8.5 } }, { runtime: 1140, title: 'Baseball', imdb: { rating: 9.1 } } ]
按数组类型查询
movies集合将电影类型作为genres字段中的数组存储。以下查询返回 genres字段为数组的文档:
db.movies.find( { genres: { $type: "array" }, runtime: { $gt: 1000 } }, { _id: 0, title: 1, genres: 1 } )
[ { title: 'Centennial', genres: [ 'Action', 'Adventure', 'Drama' ] }, { title: 'Baseball', genres: [ 'Documentary', 'History', 'Sport' ] } ]
按 MinKey 和 MaxKey 进行查询
以下示例使用 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 : "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:00.000Z'), grade: 'C', score: 15 }, { date: ISODate('2013-09-11T00:00:00.000Z'), grade: 'C', score: 16 }, { date: ISODate('2013-01-24T00:00:00.000Z'), grade: MinKey(), score: 30 }, { date: ISODate('2011-11-23T00:00:00.000Z'), grade: 'C', score: 15 } ], name: ..., 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:00.000Z'), grade: MaxKey(), score: 2 }, { date: ISODate('2013-09-11T00:00:00.000Z'), grade: 'B', score: 6 }, { date: ISODate('2013-01-24T00:00:00.000Z'), grade: MaxKey(), score: 3 }, { date: ISODate('2011-11-23T00:00:00.000Z'), grade: 'B', score: 5 } ], name: ..., restaurant_id: '30075449' } ]