设为首页 收藏本站
查看: 1276|回复: 0

[经验分享] MongoDB初探第二篇

[复制链接]

尚未签到

发表于 2015-12-22 15:13:14 | 显示全部楼层 |阅读模式
与sql语句的简单对比  在第一篇中分享了一些MongoDB的基本知识点,因为安装运行其实都还是很轻巧的,所以对于大家上手来说应该问题不大,但是安装完成,数据库也可以连接了,但是MongoDB中是没有办法运行sql语句的。这个时候关系型数据库中的一些思维直接移植过来就不适用了,但是大道至简,其实道理还是相同的,对于的数据的操作可以通过api来完成,这个从某种程度上来说,是MongoDB的亮点也是另外一种优势。
  我简单的总结了一下常用的sql语句的一些用法在MongoDB中改怎么使用。
  首先一个很大的不同是,在MongoDB中,没有表的概念,都是以collection为基本的单位存储的。可以通过show collections来查看当前的数据库中存在的collections
  > show collections
  startup_log
  system.indexes
  system.profile
  
  我们来看看增删改查的用法。
  insert
  原本sql语句中的类似下面的语句
  insert into test values(100,'test1');
  在MongoDB中可以使用如下的方式来实现,我们多插入一些数据。
  db.test.insert({id:11,name:"test1"});
  db.test.insert({id:12,name:"test2"});
  db.test.insert({id:13,name:"test3"});
  db.test.insert({id:14,name:"test4"});
  db.test.insert({id:15,name:"test5"});
  db.test.insert({id:16,name:"test6"});
  查看一下数据的情况。
  > db.test.find();
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  { "_id" : ObjectId("550edf9b14fce649885d648b"), "id" : 13, "name" : "test3" }
  { "_id" : ObjectId("550edf9b14fce649885d648c"), "id" : 14, "name" : "test4" }
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  { "_id" : ObjectId("550edf9b14fce649885d648e"), "id" : 16, "name" : "test6" }
  这个时候查看collections会发现,已经创建好了这个collection
  > show collections
  startup_log
  system.indexes
  system.profile
  test
  还有一种插入方式,如果注意到上面的数据话,会发现有一个隐含列_id,如果需要手动指定_id列的值,可以使用save方法。
  > db.test.save({_id:100001,id:11,name:"test_new"})
  WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 100001 })
  查看新插入的数据,注意_id的值
  > db.test.find();
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  { "_id" : ObjectId("550edf9b14fce649885d648b"), "id" : 13, "name" : "test3" }
  { "_id" : ObjectId("550edf9b14fce649885d648c"), "id" : 14, "name" : "test4" }
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  { "_id" : ObjectId("550edf9b14fce649885d648e"), "id" : 16, "name" : "test6" }
  { "_id" : 100001, "id" : 11, "name" : "test_new" }
  
  delete
  
  如果需要删除_id为100001的列的话,可以使用如下的方法
  > db.test.remove({_id:100001})
  WriteResult({ "nRemoved" : 1 })
  > db.test.find();
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  { "_id" : ObjectId("550edf9b14fce649885d648b"), "id" : 13, "name" : "test3" }
  { "_id" : ObjectId("550edf9b14fce649885d648c"), "id" : 14, "name" : "test4" }
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  { "_id" : ObjectId("550edf9b14fce649885d648e"), "id" : 16, "name" : "test6" }
  
  update
  我们尝试修改name列为test3的数据,修改id为18
  > db.test.update({name:"test3"},{$set:{id:18}});
  WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  > db.test.find();
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  { "_id" : ObjectId("550edf9b14fce649885d648b"), "id" : 18, "name" : "test3" }
  { "_id" : ObjectId("550edf9b14fce649885d648c"), "id" : 14, "name" : "test4" }
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  { "_id" : ObjectId("550edf9b14fce649885d648e"), "id" : 16, "name" : "test6" }
  
  or的使用方法
  在sql where字句中,经常会有or这样的过滤条件
  我们来简单模拟一下 name为test或者name为test2的数据
  > db.test.find({"$or":[{name:"test1"},{name:"test2"}]})  
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  
  and的使用方法
  我们来模拟一下name为test1并且name为test2这样的数据,这样的数据应该不存在,以下是一个错误的例子。
  > db.test.find({name:"test1"},{name:"test2"})
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "name" : "test1" }
  正确的用法应该这么写。
  模拟name为test1并且id为11的数据
  > db.test.find({"$and":[{name:"test1"},{id:11}]})
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  
  rownum
  >  db.test.find().limit(2);
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  
  count
  > db.test.count();
  6
  
  order by 
  
  注意排序的情况
  >  db.test.find().sort({name:-1})                                            
  { "_id" : ObjectId("550edf9b14fce649885d648e"), "id" : 16, "name" : "test6" }
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  { "_id" : ObjectId("550edf9b14fce649885d648c"), "id" : 14, "name" : "test4" }
  { "_id" : ObjectId("550edf9b14fce649885d648b"), "id" : 18, "name" : "test3" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  > db.test.find().sort({name:1})  
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  { "_id" : ObjectId("550edf9b14fce649885d648b"), "id" : 18, "name" : "test3" }
  { "_id" : ObjectId("550edf9b14fce649885d648c"), "id" : 14, "name" : "test4" }
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  { "_id" : ObjectId("550edf9b14fce649885d648e"), "id" : 16, "name" : "test6" }
  
  in 的使用
  得到name在test1,test2,test3的数据 
  > db.test.find({'name' : {'$in' : ["test1", "test2", "test3"]}});
  { "_id" : ObjectId("550edf9b14fce649885d6489"), "id" : 11, "name" : "test1" }
  { "_id" : ObjectId("550edf9b14fce649885d648a"), "id" : 12, "name" : "test2" }
  { "_id" : ObjectId("550edf9b14fce649885d648b"), "id" : 18, "name" : "test3" }
  >
  
  not in的使用
  > db.test.find({'name' : {'$nin' : ["test1", "test2", "test3"]}});
  { "_id" : ObjectId("550edf9b14fce649885d648c"), "id" : 14, "name" : "test4" }
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  { "_id" : ObjectId("550edf9b14fce649885d648e"), "id" : 16, "name" : "test6" }
  
  大于等于。。的使用
  id大于14的数据
  > db.test.find({id: {$gte: 14}});
  { "_id" : ObjectId("550edf9b14fce649885d648b"), "id" : 18, "name" : "test3" }
  { "_id" : ObjectId("550edf9b14fce649885d648c"), "id" : 14, "name" : "test4" }
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  { "_id" : ObjectId("550edf9b14fce649885d648e"), "id" : 16, "name" : "test6" }
  
  like的使用 
  > db.test.find({name:/5/});
  { "_id" : ObjectId("550edf9b14fce649885d648d"), "id" : 15, "name" : "test5" }
  
  distinct
  distinct的使用,不过和sql中还是存在一定的差距,有点mapreduce的味道。
  > db.test.distinct('name');
  [ "test1", "test2", "test3", "test4", "test5", "test6" ]
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-154893-1-1.html 上篇帖子: mongodb认证源码分析——使用策略模式,网络不传输密码 下篇帖子: mongoDB初探第一篇
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表