天天看點

面試官:說一下Redis和MongoDB的差別?[通俗易懂]MongoDB文法與現有關系型資料庫SQL文法比較

大家好,又見面了,我是全棧君。

項目中用的是MongoDB,但是為什麼用其實當時選型的時候也沒有太多考慮,隻是認為資料量比較大,是以采用MongoDB。

最近又想起為什麼用MongoDB,就查閱一下,彙總彙總:

之前也用過redis,當時是用來存儲一些熱資料,量也不大,但是操作很頻繁。現在項目中用的是MongoDB,目前是百萬級的資料,将來會有千萬級、億級。

就Redis和MongoDB來說,大家一般稱之為Redis緩存、MongoDB資料庫。這也是有道有理有根據的,

Redis主要把資料存儲在記憶體中,其“緩存”的性質遠大于其“資料存儲“的性質,其中資料的增删改查也隻是像變量操作一樣簡單;

MongoDB卻是一個“存儲資料”的系統,增删改查可以添加很多條件,就像SQL資料庫一樣靈活,這一點在面試的時候很受用。

Mongodb與Redis應用名額對比

MongoDB和Redis都是NoSQL,采用結構型資料存儲。二者在使用場景中,存在一定的差別,這也主要由于

二者在記憶體映射的處理過程,持久化的處理方法不同。MongoDB建議叢集部署,更多的考慮到叢集方案,Redis

更偏重于程序順序寫入,雖然支援叢集,也僅限于主-從模式。

面試官:說一下Redis和MongoDB的差別?[通俗易懂]MongoDB文法與現有關系型資料庫SQL文法比較

MongoDB文法與現有關系型資料庫SQL文法比較

1MongoDB文法            MySql文法
 2
 3db.test.find({'name':'foobar'})             <==>          select * from test where name='foobar'
 4
 5db.test.find()                                      <==>          select *from test
 6
 7db.test.find({'ID':10}).count()             <==>          select count(*) from test where ID=10
 8
 9db.test.find().skip(10).limit(20)          <==>          select * from test limit 10,20
10
11db.test.find({'ID':{$in:[25,35,45]}})     <==>          select * from test where ID in (25,35,45)
12
13db.test.find().sort({'ID':-1})                 <==>          select * from test order by IDdesc
14
15db.test.distinct('name',{'ID':{$lt:20}}) <==>          select distinct(name) from testwhere ID<20
16
17db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})     <==>     select name,sum(marks) from testgroup by name
18
19db.test.find('this.ID<20',{name:1})    <==>           select name from test whereID<20
20
21db.test.insert({'name':'foobar','age':25})    <==>       insertinto test ('name','age') values('foobar',25)
22
23db.test.remove({})                                     <==>       delete * from test
24
25db.test.remove({'age':20})                        <==>       delete test where age=20
26
27db.test.remove({'age':{$lt:20}})                <==>        delete test where age<20
28
29db.test.remove({'age':{$lte:20}})              <==>        delete test where age<=20
30
31db.test.remove({'age':{$gt:20}})              <==>         delete test where age>20
32
33db.test.remove({'age':{$gte:20}})            <==>         delete test where age>=20
34
35db.test.remove({'age':{$ne:20}})             <==>         delete test where age!=20
36
37db.test.update({'name':'foobar'},{$set:{'age':36}})<==> update test set age=36 where name='foobar'
38
39db.test.update({'name':'foobar'},{$inc:{'age':3}})<==> update test set age=age+3 where name='foobar'
40
41模糊查詢:$regex
42
43db.test.find({"name":{$regex:"aaa"}})
44
45分組個數過濾
46
47db.getCollection('id_mapper').aggregate([{$group:{ _id :"$contract_id",count:{$sum:1}}},{$match:{count:{$gt:1}}}])
48
49判斷是否為空
50
51db.getCollection('id_mapper').find({"sinocardid":{$in:[null]}})           

複制

釋出者:全棧程式員棧長,轉載請注明出處:https://javaforall.cn/111440.html原文連結:https://javaforall.cn