Before
条件操作符
条件操作符用于比较两个表达式并从mongoDB集合中获取数据
MongoDB中条件操作符有:
-
$gt - (>) 大于

-
$lt - (<) 小于

-
$gte - (>=) 大于等于

-
$lte - (<=) 小于等于

-
$lt 和 $gt - (<) 和 (>)

-
$lte 和 $gte - (<=) 和 (>=)

查询 之 Mongo VS SQL
| Mongo | SQL |
|---|---|
| db.col.find({“grades” : {$gt : 60}}) | Select * from col where grades > 60 |
| db.col.find({grades : {$gte : 77}}) | Select * from col where grades >=77 |
| db.col.find({grades : {$lt : 60}}) | Select * from col where grades < 60 |
| db.col.find({grades : {$lte : 63}}) | Select * from col where grades <= 63 |
| db.col.find({grades : {$lt :90, $gt : 70}}) | Select * from col where grades>70 AND grades<90 |
| db.col.find({grades : {$lte :85, $gte : 63}}) | Select * from col where grades>=63 AND grades<=85 |
$type 操作符
$type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果
MongoDB 中可以使用的类型如下表所示:

$type 查询:

sort()方法
使用sort()方法对数据进行排序
可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中1 为升序排列,而-1是用于降序排列
>db.COLLECTION_NAME.find().sort({KEY:1})
本文深入探讨了MongoDB中的条件操作符,如$gt、$lt、$gte和$lte,以及$type操作符的使用方法。通过对比MongoDB与SQL的查询语法,帮助读者理解如何高效地进行数据检索。此外,还介绍了sort()方法,用于实现数据的升序和降序排序。
7221

被折叠的 条评论
为什么被折叠?



