$type

在本页面

Definition

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

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

在版本 3.2 中更改。

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

您可以为BSON type指定数字或别名

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

{ 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 类型相对应的数字。

TypeNumberAliasNotes
Double1"double"
String2"string"
Object3"object"
Array4"array"
Binary data5"binData"
Undefined6"undefined"Deprecated.
ObjectId7"objectId"
Boolean8"bool"
Date9"date"
Null10"null"
Regular Expression11"regex"
DBPointer12"dbPointer"Deprecated.
JavaScript13"javascript"
Symbol14"symbol"Deprecated.
JavaScript(带范围)15"javascriptWithScope"
32-bit integer16"int"
Timestamp17"timestamp"
64-bit integer18"long"
Decimal12819"decimal"3.4 版的新功能。
Min key-1"minKey"
Max key127"maxKey"

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

  • double

  • 32-bit integer

  • 64-bit integer

  • decimal

See 按数据类型查询

MinKey 和 MaxKey

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

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

假设data集合具有两个文档MinKeyMaxKey

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

以下查询返回zipCodeBSON类型string的所有文档:

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

这些查询返回:

{ "_id" : 1, "address" : "2030 Martian Way", "zipCode" : "90698345" }

以下查询返回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" }

通过 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字段是一个数组。

Additional Information

find(), BSON Types.