MongoDB study notes (7)-conditional operators

description

Conditional operators are used to compare two expressions and get data from mongoDB collections.

The conditional operators in MongoDB are:

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

Greater than operator (>)-$gt

If you want to get the 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;

Greater than or equal operator (>=)-$gte

If you want to get the data whose "likes" in the "col" collection is greater than or equal to 100, you can use the following command:

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

Similar to the SQL statement:

Select * from col where likes >=100;

Less than operator (<)-$lt

If you want to get the data whose "likes" in the "col" collection is less than 150, you can use the following command:

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

Similar to the SQL statement:

Select * from col where likes < 150;

Less than or equal to operator (<=)-$lte

If you want to get the data whose "likes" in the "col" collection is less than or equal to 150, you can use the following command:

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

Similar to the SQL statement:

Select * from col where likes <= 150;

Combined condition operation

1. Same attribute combination query

If you want to get the data whose "likes" in the "col" collection is greater than 100 but 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;

2. Combination query of different attributes

If you want to get the data with "likes" greater than 100 and "unlikes" less than 200 in the "col" collection , you can use the following command:

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

Guess you like

Origin blog.csdn.net/qq_14997473/article/details/92816732
Recommended