Describe
In this section, we will continue to discuss the conditional operator $type in MongoDB.
The $type operator retrieves the matching data type in the collection based on the Bson type and returns the result.
The types that can be used in MongoDB are shown in the following table: type numeric notes Double 1 String 2 Object 3 Array 4 Binary Data 5 Undefined 6 is obsolete. Object ID 7 Boolean 8 Date 9 Null Regular Expression JavaScript Symbol-JavaScript (with scope) 1 5 32-bit Timestamp 64-bit integer Min key 255 Query with-1. Max Key 127
We use the database name "Runoob" Our collection name is "Col", the following is the data we inserted.
Simple Set "col":
>db.col.insert ({
title: ' PHP Tutorial ',
Description: ' PHP is a powerful server-side scripting language for creating dynamic interactive sites. ', by
: ' Rookie tutorial ',
URL: ' http://www.runoob.com ',
Tags: [' php '],
likes:200
})
>db.col.insert ({title: ' Java Tutorial ',
Description: ' Java is a high-level programming language introduced by Sun Microsystems in May 1995. ', by
: ' Rookie tutorial ',
URL: ' http://www.runoob.com ',
Tags: [' java '],
likes:150
}
>db.col.insert ({title: ' MongoDB Tutorial ',
Description: ' MongoDB is a Nosql database ', by
: ' Rookie tutorial ',
URL: ' http:// Www.runoob.com ',
Tags: [' MongoDB '],
likes:100
})
To view data using the Find () command:
> Db.col.find ()
{"_id": ObjectId ("56066542ade2f21f36b0313a"), "title": "PHP Tutorial", "description": "PHP is a kind of create dynamic interactivity A powerful server-side scripting language for the site. ", by": "Rookie Tutorial", "url": "Http://www.runoob.com", "tags": ["PHP"], "likes": "
{_id": ObjectId ("56066549ade 2f21f36b0313b ")," title ":" Java Tutorial "," description ":" Java is a high-level programming language introduced by Sun Microsystems in May 1995. " ", by": "Rookie Tutorial", "url": "Http://www.runoob.com", "tags": ["Java"], "likes": "The" "
_id": ObjectId ("5606654fad e2f21f36b0313c ")," title ":" MongoDB Tutorial "," description ":" MongoDB is a Nosql database "," by ":" Rookie Tutorial "," url ":" Http://www.runo Ob.com "," tags ": [" MongoDB "]," likes ": 100}
MongoDB operator-$type instance
If you want to get the data named String in the "Col" collection, you can use the following command:
Db.col.find ({"title": {$type: 2}})
The output results are:
{"_id": ObjectId ("56066542ade2f21f36b0313a"), "title": "PHP Tutorial", "description": "PHP is a powerful server-side scripting language for creating dynamic interactive sites." ", by": "Rookie Tutorial", "url": "Http://www.runoob.com", "tags": ["PHP"], "likes": "{_id": ObjectId ("56066549ade2f21 f36b0313b ")," title ":" Java Tutorial "," description ":" Java is a high-level programming language introduced by Sun Microsystems in May 1995. " ", by": "Rookie Tutorial", "url": "Http://www.runoob.com", "tags": ["Java"], "likes": "The" "_id": ObjectId ("5606654fade2f2 1f36b0313c ")," title ":" MongoDB Tutorial "," description ":" MongoDB is a Nosql database "," by ":" Rookie Tutorial "," url ":" HTTP://WWW.RUNOOB.C Om "," tags ": [" MongoDB "]," likes ":}