MongoDB conditional operators
Conditional operators are used to compare two expressions and get data from a mongoDB collection.
In this chapter, we will discuss how to use conditional operators in MongoDB.
The conditional operators in MongoDB are:
(>) greater than - $gt
(<) less than - $lt
(>=) Greater than or equal to - $gte
(<= ) Less than or equal to - $lte
The database name we use is "php" and our collection name is "col". The following is the data we inserted.
In order to facilitate testing, we can first use the following command to clear the data of the collection "col":
db.col.remove({})
Insert the following data
>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 })
Use the find() command to view 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 }
MongoDB (>) greater than operator - $gt
If you want to get the "likes" in the "col" collection greater than For data of 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 教程", "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 } >
MongoDB (>=) is greater than or equal to Operator - $gte
If you want to get the data with "likes" greater than or equal to 100 in the "col" collection, you can use the following command:
db.col.find({likes : {$gte : 100}})
Similar to the SQL statement:
Select * from col where likes >=100;
Output result:
> db.col.find({likes : {$gte : 100}}) { "_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 } >
MongoDB (<) Less than operator - $lt
If you want to get "col" For data with "likes" less than 150 in the collection, you can use the following command:
db.col.find({likes : {$lt : 150}})
Similar to SQL statement:
Select * from col where likes < 150;
Output result:
> db.col.find({likes : {$lt : 150}}) { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "mongodb" ], "likes" : 100 }
MongoDB (<=) less than operator - $lte
If you want to get the data of "likes" less than or equal to 150 in the "col" collection, you can use the following command:
db.col.find({likes : {$lte : 150}})
Similar to SQL statements:
Select * from col where likes <= 150;
Output results:
> db.col.find({likes : {$lte : 150}}) { "_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 }
MongoDB uses (<) and (>) queries - $lt and $gt
If you want to get the data with "likes" greater than 100 and less than 200 in the "col" collection, 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 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "java" ], "likes" : 150 } >