HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Conditional operator used to compare two expressions and obtain data from mongoDB collection.
In this section, we will discuss how to use the conditions in MongoDB operator.
MongoDB conditional operator are:
We use the database name is "w3resource" Our collection name is "col", insert the following for our data.
To facilitate testing, we can first use the following command to clear the set of "col" of data:
db.col.remove({})
Insert the following data
>db.col.insert({ title: 'PHP 教程', description: 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。', by: '本教程', url: 'https://w3resource.net', tags: ['php'], likes: 200 })
>db.col.insert({title: 'Java 教程', description: 'Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。', by: '本教程', url: 'https://w3resource.net', tags: ['java'], likes: 150 })
>db.col.insert({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: '本教程', url: 'https://w3resource.net', tags: ['mongodb'], likes: 100 })
Use find () command to view the data:
> db.col.find() { "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "php" ], "likes" : 200 } { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "java" ], "likes" : 150 } { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "mongodb" ], "likes" : 100 }
If you want to get "col" collection "likes" more than 100 data, you can use the following command:
db.col.find({"likes" : {$gt : 100}})
Similar to the SQL statement:
Select * from col where likes > 100;
Output:
> db.col.find({"likes" : {$gt : 100}}) { "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "php" ], "likes" : 200 } { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "java" ], "likes" : 150 } >
If you want to get "col" collection "likes" not less than 100 data, you can use the following command:
db.col.find({likes : {$gte : 100}})
Similar to the SQL statement:
Select * from col where likes >=100;
Output:
> db.col.find({likes : {$gte : 100}}) { "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "php" ], "likes" : 200 } { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "java" ], "likes" : 150 } { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "mongodb" ], "likes" : 100 } >
If you want to get "col" collection "likes" less than 150 data, you can use the following command:
db.col.find({likes : {$lt : 150}})
Similar to the SQL statement:
Select * from col where likes < 150;
Output:
> db.col.find({likes : {$lt : 150}}) { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "mongodb" ], "likes" : 100 }
If you want to get "col" collection "likes" less than equal to 150 data, you can use the following command:
db.col.find({likes : {$lte : 150}})
Similar to the SQL statement:
Select * from col where likes <= 150;
Output:
> db.col.find({likes : {$lte : 150}}) { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "java" ], "likes" : 150 } { "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "mongodb" ], "likes" : 100 }
If you want to get "col" collection "likes" more than 100, 200 less than the data, 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:
> db.col.find({likes : {$lt :200, $gt : 100}}) { "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "本教程", "url" : "https://w3resource.net/", "tags" : [ "java" ], "likes" : 150 } >