MongoDB conditional operator

Source: Internet
Author: User
Tags mongodb collection mongodb tutorial php tutorial

Describe

The conditional operator is used to compare two expressions and fetch data from a MongoDB collection.

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

The condition operators in MongoDB are:

    • (>) Greater than-$GT
    • (<) less than-$lt
    • (>=) greater than or equal to-$gte
    • (<=) less than or equal to-$lte

We used the database name "W3cschooldb", our collection name is "Col", the following is the data we inserted.

To facilitate testing, we can use the following command to empty the collection "col" Data:

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 interactive sites. ', by    : ' W3cschool ',    URL: ' http://www.w3cschool.cn ',    Tags: [' php '],    likes:200})

>db.col.insert ({title: ' Java Tutorial ',     Description: ' Java is a high-level programming language introduced by Sun Microsystems Company in May 1995. ', by    : ' W3cschool ',    URL: ' http://www.w3cschool.cn ',    Tags: [' java '],    likes:150})

>db.col.insert ({title: ' MongoDB Tutorial ',     Description: ' MongoDB is a Nosql database ' by    : ' W3cschool ',    URL: ' http ://www.w3cschool.cn ',    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 strong, dynamic and interactive site Server-side scripting language for power. "," by ":" W3cschool "," url ":" http://www.w3cschool.cn "," tags ": [" PHP "]," likes ":"} {"_id": ObjectId ("56066549a de2f21f36b0313b ")," title ":" Java Tutorial "," description ":" Java is a high-level programming language introduced by Sun Microsystems Company in May 1995. "," by ":" W3cschool "," url ":" http://www.w3cschool.cn "," tags ": [" Java "]," likes ":" {"} {" _id ": ObjectId (" 5606654f ade2f21f36b0313c ")," title ":" MongoDB Tutorial "," description ":" MongoDB is a Nosql database "," by ":" W3cschool "," url ":" http://w ww.w3cschool.cn "," tags ": [" MongoDB "]," likes ": 100}

MongoDB (>) greater than Operator-$GT

If you want to get more than 100 of the "likes" data 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:

MongoDB (>=) greater than equals 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:

MongoDB (<) less than operator-$LT

If you want to get data that is less than 150 "likes" 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:) {"_id": ObjectId ("5606654fade2f21f36b0313c"), "title": "MongoDB Tutorial", "Descriptio  N ":" MongoDB is a Nosql database "," by ":" W3cschool "," url ":" http://www.w3cschool.cn "," tags ": [" MongoDB "]," likes ": 100 }

MongoDB (<=) less than operator-$lte

If you want to obtain data that is less than or equal to 150 of "likes" 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: $}}) {"_id": ObjectId ("56066549ade2f21f36b0313b"), "title": "Java Tutorial", "description" : "Java is a high-level programming language introduced by Sun Microsystems Company in May 1995. "," by ":" W3cschool "," url ":" http://www.w3cschool.cn "," tags ": [" Java "]," likes ":" {"} {" _id ": ObjectId (" 5606654f ade2f21f36b0313c ")," title ":" MongoDB Tutorial "," description ":" MongoDB is a Nosql database "," by ":" W3cschool "," url ":" http://w ww.w3cschool.cn "," tags ": [" MongoDB "]," likes ": 100}

MongoDB Use (<) and (>) queries-$lt and $GT

If you want to get "likes" in the "Col" collection greater than 100, less than 200 of the data, you can use the following command:

Db.col.find ({likes: {$lt: $, $GT: 100}})

Similar to the SQL statement:

Select * from Col where likes>100 and  likes<200;

Output Result:

MongoDB conditional operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.