Easy Tutorial
❮ Mongodb Window Install Php7 Mongdb Tutorial ❯

MongoDB Conditional Operators

Description

Conditional operators are used to compare two expressions and retrieve data from MongoDB collections.

In this section, we will discuss how to use conditional operators in MongoDB.

MongoDB conditional operators include:

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

To facilitate testing, we can first use the following command to clear the data in the "col" collection:

db.col.remove({})

Insert the following data:

> 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
})

Use the find() command to view the data:

> 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 (>) Greater Than Operator - $gt

If you want to retrieve data from the "col" collection where "likes" is greater than 100, you can use the following command:

db.col.find({likes : {$gt : 100}})

Similar to the SQL statement:

Select * from col where likes > 100;

Output result:

> db.col.find({likes : {$gt : 100}})
{ "_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 Usage of (<) and (>) Queries - $lt and $gt

If you want to retrieve data from the "col" collection where "likes" is greater than 100 and less than 200, you can use the following command:

db.col.find({likes : {$lt :200, $gt : 100}})


Similar to the SQL statement:

Select * from col where likes>100 AND likes<200;


Output result:

db.col.find({likes : {$lt :200, $gt : 100}}) { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java Tutorial", "description" : "Java is a high-level programming language originally developed by Sun Microsystems and released in 1995.", "by" : "tutorialpro.org", "url" : "http://www.tutorialpro.org", "tags" : [ "java" ], "likes" : 150 }

```

❮ Mongodb Window Install Php7 Mongdb Tutorial ❯