对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$type(查询谓词运算操作符)

$type

$type selects documents where the value of the field is an instance of the specified BSON type(s). Querying by data type is useful for unstructured data where data types aren't predictable.

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

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

A $type expression for a single BSON type has the following syntax:

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

You can specify either the number or alias for the BSON type.

The $type expression can also accept an array of BSON types and has the following syntax:

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

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

有关示例,请参阅按多种数据类型进行查询。

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

提示

$type returns documents where the BSON type of the field matches the BSON type passed to $type.

For documents where field is an array, $type returns documents in which at least one array element matches a type passed to $type.

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

The $type operator accepts string aliases for the BSON types in addition to the numbers corresponding to the BSON types.

$type supports the number alias, which matches against the following BSON types:

For examples, see Examples.

提示

$isNumber

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

Querying for minKey or maxKey with $type only returns fields that match the special MinKey or MaxKey values.

示例,以下 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。

This query specifies the type with the number of the BSON type.

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' ]
}
]

以下示例使用 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'
}
]