MongoDB $type 操作符

2020-07-23  本文已影响0人  薥劃

$type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。

类型 数字 备注
Double 1
String 2
Object 3
Array 4
Binary data 5
Undefined 6 已废弃。
Object id 7
Boolean 8
Date 9
Null 10
Regular Expression 11
JavaScript 13
Symbol 14
JavaScript (with scope) 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255 Query with -1.
Max key 127

使用find()命令查看数据:

> db.col.find().pretty()
{
        "_id" : ObjectId("5f18f92eb5775c49914787b3"),
        "title" : "我是一个标题",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "tre",
        "url" : "http://www.tre.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 90
}
{
        "_id" : ObjectId("5f18f9d8b5775c49914787b4"),
        "title" : "我是一个标题",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "tre",
        "url" : "http://www.tre.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}
{
        "_id" : ObjectId("5f18f9dab5775c49914787b5"),
        "title" : "我是一个标题",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "tre",
        "url" : "http://www.tre.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 110
}
MongoDB 操作符 - $type 实例

db.col.find({"title" : {$type : 2}})

db.col.find({"title" : {$type : 'string'}})

输出结果为:

> db.col.find({"title":{$type:'string'}}).pretty()
{
        "_id" : ObjectId("5f18f92eb5775c49914787b3"),
        "title" : "我是一个标题",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "tre",
        "url" : "http://www.tre.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 90
}
{
        "_id" : ObjectId("5f18f9d8b5775c49914787b4"),
        "title" : "我是一个标题",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "tre",
        "url" : "http://www.tre.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}
{
        "_id" : ObjectId("5f18f9dab5775c49914787b5"),
        "title" : "我是一个标题",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "tre",
        "url" : "http://www.tre.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 110
}
上一篇下一篇

猜你喜欢

热点阅读