Easy Tutorial
❮ Mongodb Query Mongodb Connections ❯

MongoDB $type Operator

Description

In this section, we will continue to discuss the MongoDB conditional operator $type.

The $type operator retrieves data from the collection based on BSON type and returns the results.

The types available in MongoDB are shown in the table below:

Type Number Notes
Double 1
String 2
Object 3
Array 4
Binary data 5
Undefined 6 Deprecated.
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

The database name we use is "tutorialpro" and our collection name is "col". The following is the data we inserted.

Simple collection "col":

>db.col.insert({
    title: 'PHP Tutorial', 
    description: 'PHP is a powerful server-side scripting language for creating dynamic and interactive websites.',
    by: 'tutorialpro.org',
    url: 'http://www.tutorialpro.org',
    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: 'tutorialpro.org',
    url: 'http://www.tutorialpro.org',
    tags: ['java'],
    likes: 150
})
>db.col.insert({title: 'MongoDB Tutorial', 
    description: 'MongoDB is a NoSQL database.',
    by: 'tutorialpro.org',
    url: 'http://www.tutorialpro.org',
    tags: ['mongodb'],
    likes: 100
})

View data using the find() command:

> db.col.find()
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP Tutorial", "description" : "PHP is a powerful server-side scripting language for creating dynamic and interactive websites.", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java Tutorial", "description" : "Java is a high-level programming language introduced by Sun Microsystems in May 1995.", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB Tutorial", "description" : "MongoDB is a NoSQL database.", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "mongodb" ], "likes" : 100 }

MongoDB Operator - $type Example

If you want to retrieve data from the "col" collection where the title is of type String, you can use the following command:


{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP Tutorial", "description" : "PHP is a powerful server-side scripting language for creating dynamic and interactive websites.", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "php" ], "likes" : 200 } { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java Tutorial", "description" : "Java is a high-level programming language introduced by Sun Microsystems in May 1995.", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "java" ], "likes" : 150 } { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB Tutorial", "description" : "MongoDB is a NoSQL database", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "mongodb" ], "likes" : 100 } ```

❮ Mongodb Query Mongodb Connections ❯