3.17. MongoDB conditional operator

发布时间 : 2025-10-25 13:34:23 UTC      

Page Views: 9 views

3.17.1. Description

The conditional operator is used to compare two expressions and get data from the mongoDB collection.

In this section, 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

我们使用的数据库名称为”runoob” 我们的集合名称为”col”,以下为我们插入的数据。

To facilitate testing, we can first clear the data for the collection “col” using the following command:

db.col.remove({}) 

Insert the following data

>db.col.insert({ title: 'PHP 教程', description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。', by: '菜鸟教程', url: 'http://www.runoob.com', tags: ['php'], likes: 200 }) 
>db.col.insert({title: 'Java 教程', description: 'Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。', by: '菜鸟教程', url: 'http://www.runoob.com', tags: ['java'], likes: 150 }) 
>db.col.insert({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: ['mongodb'], likes: 100 }) 

Use the find () command to view the data:

> db.col.find() { "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "php" ], "likes" : 200 } { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 } { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 } 

3.17.2. MongoDB (>) is greater than the operator-$gt

If you want to get data with “likes” greater than 100 in the “col” collection, 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" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "php" ], "likes" : 200 } { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 } > 

3.17.3. MongoDB (> =) is greater than or equal to the operator-$gte

If you want to get 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" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "php" ], "likes" : 200 } { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 } { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 } > 

3.17.4. MongoDB (<) is less than the operator-$lt

If you want to get data with “likes” less than 150 in the “col” collection, you can use the following command:

db.col.find({likes : {$lt : 150}}) 

Similar to the 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" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 } 

3.17.5. MongoDB (< =) less than or equal to operator-$lte

If you want to get data with “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 the SQL statement:

Select * from col where likes <= 150; 

Output result:

> db.col.find({likes : {$lte : 150}}) { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 } { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 } 

3.17.6. MongoDB uses (<) and (>) queries-$lt and $gt

If you want to get data with “likes” greater than 100and less than 200in 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" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 } > 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。