Tutorial MongoD...LOG MASUK
Tutorial MongoDB
pengarang:php.cn  masa kemas kini:2022-04-21 17:49:03

Pengendali $jenis MongoDB


Penerangan

Dalam bab ini, kita akan terus membincangkan operator bersyarat $type dalam MongoDB.

Pengendali $type adalah berdasarkan jenis BSON untuk mendapatkan semula jenis data yang sepadan dalam koleksi dan mengembalikan hasilnya.

Jenis yang boleh digunakan dalam MongoDB ditunjukkan dalam jadual berikut:

类型数字备注
Double1 
String2 
Object3 
Array4 
Binary data5 
Undefined6已废弃。
Object id7 
Boolean8 
Date9 
Null10 
Regular Expression11 
JavaScript13 
Symbol14 
JavaScript (with scope)15 
32-bit integer16 
Timestamp17 
64-bit integer18 
Min key255Query with -1.
Max key127 

Nama pangkalan data yang kami gunakan ialah "php" dan nama koleksi kami ialah "col" . Berikut ialah data Dimasukkan kami.

Koleksi ringkas "col":

>db.col.insert({
    title: 'PHP 教程', 
    description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。',
    by: 'php中文网',
    url: 'http://www.php.cn',
    tags: ['php'],
    likes: 200
})

>db.col.insert({title: 'Java 教程', 
    description: 'Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。',
    by: 'php中文网',
    url: 'http://www.php.cn',
    tags: ['java'],
    likes: 150
})

>db.col.insert({title: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: 'php中文网',
    url: 'http://www.php.cn',
    tags: ['mongodb'],
    likes: 100
})

Gunakan arahan find() untuk melihat data:

> db.col.find()
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "mongodb" ], "likes" : 100 }

Pengendali MongoDB - contoh $type

Jika anda ingin mendapatkan data yang tajuknya String dalam koleksi "col", anda boleh menggunakan arahan berikut:

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

Hasil output ialah:

{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "mongodb" ], "likes" : 100 }

Laman web PHP Cina