The conditional operator is used to compare two expressions and fetch data from a MongoDB collection.
The condition operators in MongoDB are:
- (>) Greater than-$GT
- (<) less than-$lt
- (>=) greater than or equal to-$gte
- (<=) less than or equal to-$lte
> Db.new.find ({"Age": {$gt: +}}) #查找age大于22的文档 > Db.new.find ({"Age": {$lt:]}) #查找age小于22的文档 > Db.new.find ({" Age ": {$gte: $}) #查找age大于等于22的文档 > Db.new.find ({" Age ": {$lte:]}) #查找age小于等于22的文档 <pre name=" code "class=" plain ">> Db.new.find ({" Age ": {$lte: $gt:}) #查找age小于等于27, documents greater than 22.
The $type operator is based on the Bson type to retrieve the matching results in the collection.
Types that can be used in MongoDB:
Type Description |
Type value |
Double |
1 |
String |
2 |
Object |
3 |
Array |
4 |
Binary data |
5 |
Object ID |
7 |
Boolean |
8 |
Date |
9 |
Null |
10 |
Regular expression |
11 |
JavaScript Code |
13 |
Symbol |
14 |
JavaScript Code with scope |
15 |
32-bit integer |
16 |
Timestamp |
17 |
64-bit integer |
18 |
Min Key |
255 |
Max Key |
127 |
>db.new.find ({"Age": {$type: +}}) #查找age为double型的所有数据
Novice MongoDB Learning---(iv) MONGODB conditional operator