zhangsharp20
作者zhangsharp20·2015-06-05 13:51
数据库运维工程师·外管

MongoDB初探第二篇

字数 6644阅读 654评论 0赞 0

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

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广