MySQL與MongoDB查詢互轉


Mysql與MongoDB查詢互轉

mongo查詢嚴格要求數據格式!

1、只想查出某些數據,不想全部數據都查出來

mysql:
select name from user;

mongo:
db.user.find(
    {},
    {
    _id : 0,
    name : 1
   }
)

 

說明:user是集合的名稱,find里面兩個{},第一個留空是想所有數據都查出來,加條件的話類型mysql的where,第二個{}表示的意思類似mysql后面的select部分,0代表不顯示,1代表顯示。

 

2、分頁查詢

mysql:
select * from user limit 0,10;

mongo:
db.user.find({}).skip(0).limit(10)

 

說明:mongo的skip和limit與mysql同理,mysql的limit第一個參數是跳過的數據量與mongo的skip類似,比如第三頁的數據是從20開始的,mysql:limit 20,10,即:limit (page-1)*size,size

 

3、條件查詢

mysql:
select name from user where id = 1;

mongo:
db.user.find(
    { id : 1 },
    { name : 1 }
)

 

說明:由於有嚴格要求數據格式,若存到mongo的id是字符串格式的話,查詢的條件得加上雙引號""

 

4、范圍查詢

MySQL MongoDB remark
> $gt 大於
< $lt 小於
>= $gte 大於等於
<= $lte 小於等於
!= $ne 不等於

 

 

mysql:
select name from user where id > 1 and id < 10;

mongo:
db.user.find(
    {
        id : {
            $gt : 1,
            $lt : 10
        }
    },
    { name : 1 }
)

 

說明:mysql的between其實就是>=和<=,字符串的話用范圍查詢好像會有問題,慎用!

 

5、in查詢

mysql:
select name from user where id in (1,2);

mongo:
db.user.find(
    {
        id : {
            $in : [1, 2]
        }
    },
    { name : 1 }
)

 

說明:not in查詢就把$in換成$nin

 

6、條件統計count

mysql:
select count(*) from user where id > 1;

mongo:
db.user.find(
    {
        id :  {
            $gt: 1
        }
    }
).count()

 

7、all查詢

mongo可以將數組存儲起來,若想查詢某個字段(是個數組)同時包含值a和b

db.user.find(
    {
        detail: {
            $all : ["7", "8"]
        }
    }
)

 

說明:這個查詢的結果集,detail字段同時包含字符串7和字符串8

 

8、exists查詢
比如我想找出所有包含字段name_real的結果集

db.user.find(
    {
        name_real : {
            $exists : true
        }
    }
)


說明:上面查詢的結果中,所有數據肯定都包含有name_real字段;改成false的話就變成不包含

 

9、is null查詢

mysql:
select * from user where age is null;

mongo:
db.user.find(
    { age : null }
)

 

但是這樣會有問題,這個查詢會把那些沒有age字段的結果也查出來,結合exists優化下

db.user.find(
    {
        age: {
            $in : [null], 
            $exists : true
        }
    }
)

 

查詢is not null

db.user.find(
    {
        age: {
            $ne : null,
            $exists : true
        }
    }
)

 

10、取模運算
mongo提供取模運算,比如存儲了一些數據,我想查出那些取模后等於某個值的數據可以使用$mod
比如下例查詢年齡是10的倍數的用戶

mysql:
select * from user where age % 10 = 0;
mongo:
db.user.find(
    {
        age:{
            $mod : [ 10 , 0 ]
        }
    }
)

 

11、查詢數據元素個數
由於mongo可以存儲數組,如果想查詢數組中只有兩個元素的記錄時,可以使用$size
比如下例查詢有三個興趣愛好的用戶

db.user.find(
    {
        favorite: {
            $size: 3
        }
    }
)

 

12、正則匹配查詢
如果想用正則匹配查詢,可以使用$regex
比如下例匹配年齡是10以下的用戶

db.user.find(
    {
        age: {
            $regex: /^([1-9])$/
        }
    }
)

 

13、只取一部分數據
類似mysql的limit,mongo也可以只取一部分數據

mysql:
select * from user limit 10;
mongo:
db.user.find().limit(10)

 

14、排序

MySQL MongoDB 說明
asc 1 升序
desc -1 降序



mysql:
select * from user order by age asc;
mongo:
db.user.find().sort(
    {age: 1}
)

 

說明:mongo字符串類型的也可以排序

15、求和
直接累加求和某一項
比如下例對年齡進行求和

mysql:
select sum(age) as total from user;
mongo:
db.user.aggregate([
    {
        $group: 
        {
            _id: null,
            total: {
                $sum: "$age" 
            }
        }
    }
])

 

分組求和
下例為按類型分組並求和

mysql:
select type,sum(age) as total from user group by type;
mongo:
db.user.aggregate([
    {
        $group: 
        {
            _id: "$type",
            total: {
                $sum: "$age" 
            }
        }
    }
])

 

多條件分組求和
下例為按多個條件進行分組並求和

mysql:
select type,sex,sum(age) as total from user group by type,sex;
mongo:
db.user.aggregate([
    {
        $group: {
            _id:{
                type: "$type",
                sex: "$sex"
            },
            total: {
                $sum: "$age" 
            }
        }
    }
])

 

16、分組后having
下例為按條件分組並篩選出求和后大於100的數據

mysql:
select type, sum(age) as total from user group by type having total > 100;
mongo:
db.user.aggregate([
    {
        $group: 
        {
            _id: "$type",
            total: {
                $sum: "$age" 
            }
        }
    },
    {
        $match: {
            total: { 
                $gt: 100 
            }
        }
    }
])

 

17、條件分組
類似mysql的where+group by進行查詢
下例為查找出2020-01-01(timestamp:1577808000)后注冊的用戶,並按類型分組求和

mysql:
select type,sum(age) as total from user where created > 1577808000 group by type;
mongo:
db.user.aggregate([
    {
        $match: {
           created: { $gt: 1577808000 }
           }
    },
    {
         $group: {
            _id: "$type",
            total: { $sum: "$age" }
         }
       }
])

 

條件分組並having篩選
下例為查找出2020-01-01(timestamp:1577808000)后注冊的用戶,並按類型分組,同時篩選出大於100的數據

mysql:
select type,sum(age) as total from user where created > 1577808000 group by type having total > 100;
mongo:
db.user.aggregate([
    {
           $match: {
               created: { $gt: 1577808000 }
          }
       },
       {
         $group: {
            _id: "$type",
            total: { $sum: "$age" }
         }
       },
       {
          $match: {
              total: { $gt: 100 }
          }
       }
])

 

18、unwind
加入你的mongo的每一條記錄有一個字段,存的是一個數組,數組里面是對象,類似這樣,article字段含有

[
    { "uid" : 1, "title" : "XXX", "content" : "XXX", "views" : 10 },
    { "uid" : 2, "title" : "XXX", "content" : "XXX", "views" : 11 },
    { "uid" : 3, "title" : "XXX", "content" : "XXX", "views" : 12 }
]

 

使用unwind可以使上面原本一條記錄進行展開,分為三條數據進行展示,有點像mysql的join查詢,只不過mysql得分開兩個表存

mysql:
select * from user as u left join article as a on (u.id=a.uid);
mongo:
db.user.aggregate([
   { $unwind: "$article" }
])

 

unwind后求和

mysql:
select sum(views) as total from user as u left join article as a on (u.id=a.uid)) as data
mongo:
db.user.aggregate([

    { $unwind: "$article" },
    {
        $group: {
            _id: null,
            total: { $sum: "$article.views" }
         }
       }
])

 

19、分組后統計總共有多少組
下例分按類型分組,並統計總數

mysql:
select count(*) from (select type from user group by type);
mongo:
db.user.aggregate([
       {
           $group: {
            _id: "$type"
        }
    },
    {
        $group: 
           {
              _id : null,
            count: { $sum: 1 }
           }
       }
])

 

20、aggregate類型linux的grep指令,像管道處理一樣,一級接一級,比如:篩選、分組、過濾等,最后返回結果

db.user.aggregate([
    { $match: { sex: "boy" } },
       { $group: { _id: "$type", total: { $sum: "$age" } } }
])

 

 

未完。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM