前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >面试官:说一下Redis和MongoDB的区别?

面试官:说一下Redis和MongoDB的区别?

作者头像
码农编程进阶笔记
发布2021-09-29 15:50:47
3.2K0
发布2021-09-29 15:50:47
举报

项目中用的是MongoDB,但是为什么用其实当时选型的时候也没有太多考虑,只是认为数据量比较大,所以采用MongoDB。

最近又想起为什么用MongoDB,就查阅一下,汇总汇总:

之前也用过redis,当时是用来存储一些热数据,量也不大,但是操作很频繁。现在项目中用的是MongoDB,目前是百万级的数据,将来会有千万级、亿级。

就Redis和MongoDB来说,大家一般称之为Redis缓存、MongoDB数据库。这也是有道有理有根据的,

Redis主要把数据存储在内存中,其“缓存”的性质远大于其“数据存储“的性质,其中数据的增删改查也只是像变量操作一样简单;

MongoDB却是一个“存储数据”的系统,增删改查可以添加很多条件,就像SQL数据库一样灵活,这一点在面试的时候很受用。

Mongodb与Redis应用指标对比

MongoDB和Redis都是NoSQL,采用结构型数据存储。二者在使用场景中,存在一定的区别,这也主要由于 二者在内存映射的处理过程,持久化的处理方法不同。MongoDB建议集群部署,更多的考虑到集群方案,Redis 更偏重于进程顺序写入,虽然支持集群,也仅限于主-从模式。

MongoDB语法与现有关系型数据库SQL语法比较

代码语言:javascript
复制
MongoDB语法            MySql语法

db.test.find({'name':'foobar'})             <==>          select * from test where name='foobar'

db.test.find()                                      <==>          select *from test

db.test.find({'ID':10}).count()             <==>          select count(*) from test where ID=10

db.test.find().skip(10).limit(20)          <==>          select * from test limit 10,20

db.test.find({'ID':{$in:[25,35,45]}})     <==>          select * from test where ID in (25,35,45)

db.test.find().sort({'ID':-1})                 <==>          select * from test order by IDdesc

db.test.distinct('name',{'ID':{$lt:20}}) <==>          select distinct(name) from testwhere ID<20

db.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

db.test.find('this.ID<20',{name:1})    <==>           select name from test whereID<20

db.test.insert({'name':'foobar','age':25})    <==>       insertinto test ('name','age') values('foobar',25)

db.test.remove({})                                     <==>       delete * from test

db.test.remove({'age':20})                        <==>       delete test where age=20

db.test.remove({'age':{$lt:20}})                <==>        delete test where age<20

db.test.remove({'age':{$lte:20}})              <==>        delete test where age<=20

db.test.remove({'age':{$gt:20}})              <==>         delete test where age>20

db.test.remove({'age':{$gte:20}})            <==>         delete test where age>=20

db.test.remove({'age':{$ne:20}})             <==>         delete test where age!=20

db.test.update({'name':'foobar'},{$set:{'age':36}})<==> update test set age=36 where name='foobar'

db.test.update({'name':'foobar'},{$inc:{'age':3}})<==> update test set age=age+3 where name='foobar'

模糊查询:$regex

db.test.find({"name":{$regex:"aaa"}})

分组个数过滤

db.getCollection('id_mapper').aggregate([{$group:{ _id :"$contract_id",count:{$sum:1}}},{$match:{count:{$gt:1}}}])

判断是否为空

db.getCollection('id_mapper').find({"sinocardid":{$in:[null]}})
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-09-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码农编程进阶笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Mongodb与Redis应用指标对比
  • MongoDB语法与现有关系型数据库SQL语法比较
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档