前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB初探第二篇 (r4笔记第82天)

MongoDB初探第二篇 (r4笔记第82天)

作者头像
jeanron100
发布2018-03-15 16:45:45
4780
发布2018-03-15 16:45:45
举报

与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" ]

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 杨建荣的学习笔记 微信公众号,前往查看

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

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

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