在 MongoDB 中,$type 操作符用于选择那些具有指定 BSON 类型的字段的文档。BSON 是一种二进制的 JSON 表示形式,MongoDB 使用它来存储数据。每种数据类型在 BSON 中都有一个对应的数字代码。$type 操作符允许你基于这些类型来过滤文档。
语法:
db.collection.find({field:{$type: <type> ]}})
- filed: 要检查类型的字段
- type: 指定的BSON类型,可以是类型的数字代码或类型名称的字符串
BSON类型
| 类型代码 | 类型名称 |
|---|---|
| 1 | double |
| 2 | string |
| 3 | object |
| 4 | array |
| 5 | binData |
| 6 | undefined |
| 7 | objectId |
| 8 | bool |
| 9 | date |
| 10 | null |
| 11 | regex |
| 12 | dbPointer |
| 13 | javascript |
| 14 | symbol |
| 15 | javascriptWithScope |
| 16 | int |
| 17 | timestamp |
| 18 | long |
| 19 | decimal |
| 255 | minKey |
| 127 | maxKey |
查找字段类型为字符串的文档:
articledb> db.comment.find({nickname:{$type:"string"}})
[
{
_id: ObjectId('672df740edddfce8730d8190'),
articleid: '100000',
content: '今天天气真好,阳光明媚',
userid: '1001',
nickname: 'Rose',
createdatetime: ISODate('2024-11-08T11:34:24.247Z'),
likenum: 10,
state: null
},
{
_id: '2',
articleid: '100001',
content: '确实,生活中少一点物质追求,多一份对健康的关怀,简单如一杯清水也能带来极大的满足。',
userid: '1003',
nickname: '凯撒大帝',
createdatetime: ISODate('2019-06-07T10:15:30.123Z'),
likenum: 850,
state: '1'
},
{
_id: '3',
articleid: '100001',
content: '赞同!与其追逐新潮电子产品,不如投资于自己的身心健康。一杯温暖的水,简单却美好。',
userid: '1004',
nickname: '云淡风轻',
createdatetime: ISODate('2019-09-20T15:45:45.678Z'),
likenum: 781,
state: '1'
},
{
_id: '4',
articleid: '100001',
content: '说得好!我们应该更加重视日常的小确幸,比如一杯干净温暖的水,它能给予我们无限的能量。',
userid: '1005',
nickname: '花开半夏',
createdatetime: ISODate('2019-11-11T07:30:15.987Z'),
likenum: 920,
state: '1'
},
{
_id: '5',
articleid: '100001',
content: '非常同意您的观点,健康才是最重要的财富。每天喝足量的水对于维持良好的身体状态至关重要。',
userid: '1006',
nickname: '月下独酌',
createdatetime: ISODate('2019-03-14T14:15:25.345Z'),
likenum: 1100,
state: '1'
},
{
_id: '6',
articleid: '100001',
content: '简单的幸福往往最珍贵。让我们从今天开始减少不必要的开支,并更加注重个人及家人的健康吧!',
userid: '1007',
nickname: '星梦奇缘',
createdatetime: ISODate('2019-12-25T00:00:00.000Z'),
likenum: 1200,
state: '1'
},
{
_id: '7',
articleid: '100001',
content: '今天,我想和大家分享一下关于健康饮食的问题。',
userid: '1008',
nickname: '风华浪尖',
createdatetime: ISODate('2019-05-05T10:30:15.123Z'),
likenum: 1300,
state: '1'
}
]
articledb>
或使用类型代码:
articledb> db.comment.find({nickname:{$type:2}})
[
{
_id: ObjectId('672df740edddfce8730d8190'),
articleid: '100000',
content: '今天天气真好,阳光明媚',
userid: '1001',
nickname: 'Rose',
createdatetime: ISODate('2024-11-08T11:34:24.247Z'),
likenum: 10,
state: null
},
{
_id: '2',
articleid: '100001',
content: '确实,生活中少一点物质追求,多一份对健康的关怀,简单如一杯清水也能带来极大的满足。',
userid: '1003',
nickname: '凯撒大帝',
createdatetime: ISODate('2019-06-07T10:15:30.123Z'),
likenum: 850,
state: '1'
},
{
_id: '3',
articleid: '100001',
content: '赞同!与其追逐新潮电子产品,不如投资于自己的身心健康。一杯温暖的水,简单却美好。',
userid: '1004',
nickname: '云淡风轻',
createdatetime: ISODate('2019-09-20T15:45:45.678Z'),
likenum: 781,
state: '1'
},
{
_id: '4',
articleid: '100001',
content: '说得好!我们应该更加重视日常的小确幸,比如一杯干净温暖的水,它能给予我们无限的能量。',
userid: '1005',
nickname: '花开半夏',
createdatetime: ISODate('2019-11-11T07:30:15.987Z'),
likenum: 920,
state: '1'
},
{
_id: '5',
articleid: '100001',
content: '非常同意您的观点,健康才是最重要的财富。每天喝足量的水对于维持良好的身体状态至关重要。',
userid: '1006',
nickname: '月下独酌',
createdatetime: ISODate('2019-03-14T14:15:25.345Z'),
likenum: 1100,
state: '1'
},
{
_id: '6',
articleid: '100001',
content: '简单的幸福往往最珍贵。让我们从今天开始减少不必要的开支,并更加注重个人及家人的健康吧!',
userid: '1007',
nickname: '星梦奇缘',
createdatetime: ISODate('2019-12-25T00:00:00.000Z'),
likenum: 1200,
state: '1'
},
{
_id: '7',
articleid: '100001',
content: '今天,我想和大家分享一下关于健康饮食的问题。',
userid: '1008',
nickname: '风华浪尖',
createdatetime: ISODate('2019-05-05T10:30:15.123Z'),
likenum: 1300,
state: '1'
}
]
articledb>
$type 操作符对于验证数据完整性、清理数据以及确保正确类型的数据被查询出来是非常有用的。