MongoDB入门实操《中篇》续

欢迎关注【无量测试之道】公众号,回复【领取资源】,
Python编程学习资源干货、
Python+Appium框架APP的UI自动化、
Python+Selenium框架Web的UI自动化、
Python+Unittest框架API自动化、

资源和代码 免费送啦~
文章下方有公众号二维码,可直接微信扫一扫关注即可。

1、MongoDB 中的操作与MysqlDB 中的查询操作对比展示

左边为Mongo 命令右边为Mysql 命令:

db.test.find()  ==> select * from test
db.test.findOne()  ==> select *from test limit 1
db.test.find().pretty()  ==>select * from test \G
db.test.find().limit(2)  ==> select * from test limit 2
db.test.find().count()  ==>select count(*) from test
db.test.find().sort({age: 1})  ==>select * from test order by age
db.test.find().sort({age: -1})  ==>select * from test order by age desc
db.test.find({"age" : 1})  ==>select * from test where age = 1
db.test.find({"age":{$gt:29}}).skip(1).limit(3)  ==> select * from test where age>29 limit 1,3;
db.test.find({"name" : "joe", "age" : 1})  ==>select * from test where "name" = "joe" and age = 1
db.test.find({}, {"name" : 1, "age" : 1})  ==>select name, age from test
db.test.find({}, {"name" : 1, "_id" : 0})  ==>select name from test # 这里相当于是不让_id显示出来
db.test.find({"age" : {"$gte" : 18, "$lte" : 30}})  ==>select * from test where age >=18 and age <= 30 // $lt(<) $lte(<=) $gt(>) $gte(>=)
db.test.find({"name" : {"$ne" : "joe"}})  ==>select * from test where name <> "joe"
db.test.find({"age" : {"$in" : [25, 42, 30]}})  ==> select * from test where age in (25, 42, 30)
db.test.find({"age" : {"$nin" : [25, 42, 30]}})  ==>select * from test where age not in (25, 42, 30)
db.test.find({"$or" : [{"age" : 25}, {"job" : "tester"}]})  ==>select * form test where age= 25 or job= "tester"
db.test.find({"$not": {"age" : 27}})  ==>select * from test where not (age = 27)
db.test.find({"name" : {"$in" : [null], "$exists" : true}})  ==> select * from test where name is null 
db.test.find({"name" : /joy?/i})  ==>select * from test where name like "%joy%”

2、Mysql和MongoDB区别以及主要应用场景

Mysql和MongoDB区别:

应用场景:
1、如果需要将MongoDB作为后端DB来代替Mysql使用,即这里Mysql与MongoDB 属于平行级别。
那么,这样的使用可能有以下几种情况的考量:
(1)、MongoDB所负责部分以文档形式存储,能够有较好的代码亲和性,JSON格式的直接写入方便。(如日志之类)
(2)、从datamodels设计阶段就将原子性考虑于其中,无需事务之类的辅助。开发用如nodejs之类的语言来进行开发,对开发比较方便。
(3)、MongoDB本身的failover机制,无需使用如MHA之类的方式实现。

2、将MongoDB作为类似redis ,memcache来做缓存DB,为Mysql提供服务,或是后端日志收集分析。考虑到MongoDB属于nosql型数据库,sql语句与数据结构不如Mysql那么亲和 ,也会有很多时候将MongoDB做为辅助Mysql而使用的类redis memcache 之类的缓存db来使用。亦或是仅作日志收集分析。

备注:我的个人公众号已正式开通,致力于测试技术的分享,包含:大数据测试、功能测试,测试开发,API接口自动化、测试运维、UI自动化测试等,微信搜索公众号:“无量测试之道”,或扫描下方二维码:

 添加关注,一起共同成长吧。

猜你喜欢

转载自blog.csdn.net/weixin_41754309/article/details/107110705