Describe
In this section, we will continue to discuss the conditional operator $type in MongoDB.
The $type operator is based on the Bson type to retrieve the matching data type in the collection and returns the result.
The types that can be used in MongoDB are shown in the following table:
type |
Digital |
Notes |
Double |
1 |
|
String |
2 |
|
Object |
3 |
|
Array |
4 |
|
Binary data |
5 |
|
Undefined |
6 |
is obsolete. |
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 |
|
We used the database name "W3cschooldb", our collection name is "Col", the following is the data we inserted.
Simple set of "col":
>db.col.insert ({ title: ' PHP Tutorial ', Description: ' PHP is a powerful server-side scripting language for creating dynamic interactive sites. ', by : ' W3cschool ', URL: ' http://www.w3cschool.cn ', Tags: [' php '], likes:200})
>db.col.insert ({title: ' Java Tutorial ', Description: ' Java is a high-level programming language introduced by Sun Microsystems Company in May 1995. ', by : ' W3cschool ', URL: ' http://www.w3cschool.cn ', Tags: [' java '], likes:150})
>db.col.insert ({title: ' MongoDB Tutorial ', Description: ' MongoDB is a Nosql database ' by : ' W3cschool ', URL: ' http://www.w3cschool.cn ', Tags: [' MongoDB '], likes:100})
Use the Find () command to view the data:
> Db.col.find () {"_id": ObjectId ("56066542ade2f21f36b0313a"), "title": "PHP Tutorial", "description": "PHP is a strong, dynamic and interactive site Server-side scripting language for power. "," by ":" W3cschool "," url ":" http://www.w3cschool.cn "," tags ": [" PHP "]," likes ":"} {"_id": ObjectId ("56066549a de2f21f36b0313b ")," title ":" Java Tutorial "," description ":" Java is a high-level programming language introduced by Sun Microsystems Company in May 1995. "," by ":" W3cschool "," url ":" http://www.w3cschool.cn "," tags ": [" Java "]," likes ":" {"} {" _id ": ObjectId (" 5606654f ade2f21f36b0313c ")," title ":" MongoDB Tutorial "," description ":" MongoDB is a Nosql database "," by ":" W3cschool "," url ":" http://w ww.w3cschool.cn "," tags ": [" MongoDB "]," likes ": 100}
MongoDB operator-$type instance
If you want to get the data in the "Col" collection with the title String, you can use the following command:
Db.col.find ({"title": {$type: 2}})
The output is:
{"_id": ObjectId ("56066542ade2f21f36b0313a"), "title": "PHP Tutorial", "description": "PHP is a powerful server-side scripting language for creating dynamic interactive sites. "," by ":" W3cschool "," url ":" http://www.w3cschool.cn "," tags ": [" PHP "]," likes ":"} {"_id": ObjectId ("56066549a de2f21f36b0313b ")," title ":" Java Tutorial "," description ":" Java is a high-level programming language introduced by Sun Microsystems Company in May 1995. "," by ":" W3cschool "," url ":" http://www.w3cschool.cn "," tags ": [" Java "]," likes ":" {"} {" _id ": ObjectId (" 5606654f ade2f21f36b0313c ")," title ":" MongoDB Tutorial "," description ":" MongoDB is a Nosql database "," by ":" W3cschool "," url ":" http://w ww.w3cschool.cn "," tags ": [" MongoDB "]," likes ": 100}
MongoDB $type Conditional operator