zoukankan      html  css  js  c++  java
  • 面试官:说一下Redis和MongoDB的区别?

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

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

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

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

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

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

    Mongodb与Redis应用指标对比

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

    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]}})

    赞赏码

    非学,无以致疑;非问,无以广识

  • 相关阅读:
    工具资源系列之给 windows 虚拟机装个 mac
    工具资源系列之 github 上各式各样的小徽章从何而来?
    php 学习笔记之日期时间操作一箩筐
    2018-12-03 VS Code英汉词典插件v0.0.7-尝试词性搭配
    2018-11-29 VS Code英汉词典插件v0.0.6-改为TS实现, 加测试
    2018-11-27 中文代码示例之Programming in Scala笔记第七八章
    2018-11-23 手工翻译Vue.js源码:尝试重命名标识符与文本
    2018-11-21 手工翻译Vue.js源码第一步:14个文件重命名
    2018-11-16 中文代码示例之Programming in Scala笔记第四五六章
    2018-11-13 中文代码示例之Programming in Scala学习笔记第二三章
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15452465.html
Copyright © 2011-2022 走看看