MongoDB快速入门——SQL与MongoDB的对照

 

       上一篇对MongoDB有了大概的介绍,没想到能到得到网友的评论,之前写了很多博客基本没人评论,所以你们的评论就是对我最大的支持。我也是那天刚接触MongoDB然后写的一篇关于MongoDB的介绍,有一个网友问我后面教程会不会持续更新,你们的评论就是对我最大的支持,所以我决定继续跟进。虽然快期末考试了,但只要有时间我会持续更新,希望大家多多支持。

       前面我们介绍了一些MongoDB的概念,这次我们来通过对比关系数据库Oracle操作,来介绍相同操作在MongoDB中如何做。

        首先介绍一些关系数据库中常用的概念对比MongoDB中与之对应的概念。

 

                                      Oracle                                                                                                          MongoDB

                                     DataBase                                                                                                      DataBase

                                     Table(表)                                                                                                       Collection(集合)

                                    index(索引)                                                                                                      index(索引)

                                     row(一行记录)                                                                                  BSON(类似JSON格式) http://bsonspec.org/

                                      column(列,字段)                                                                                          BSON中的字段

                                      join(连接)                                                                                  embedding and linking(嵌入和连接)

                                     primary key(主键)                                                                                       _id field(ID标识符)

                                      group by(分组)                                                                                        aggregation(聚合)

                                      其实在学习MongoDB过程中我们就是要忘记模式,记住”键值对”就可以啦。

                       MongoDB的查询是通过JSON(BSON)对象,来表示的。接下来我们将通过对比展示SQL和MonggoDB的查询语法

 

                                                                SQL语句                                                                                             MongoDB语句

                Create table users(a int,b int)建立一张表                     我们无需显式创建Collection,前面讲了在我们保存第一条文档的时候MongoDB会自动创建,当然我们也可以显示的建立:
Collection: db.createCollection("users", { capped:true, size:100000}) // capped:是否覆盖 size:大小以字节为单位

 

                             Alter table users………                                                                                                                 模式自由

 

inert into users value(3,5)

db.users.insert({a:3,b:5})

 

 

select a,b from users

db.users.find({}, {a:1,b:1})

select * from users

db.users.find()

select * from users where age=33

db.users.find({age:33})

select a,b from users where age=33

db.users.find({age:33}, {a:1,b:1})

select * from users where age=33 order by name

db.users.find({age:33}).sort({name:1})

select * from users where age>33

db.users.find({age:{$gt:33}})

select * from users where age!=33

db.users.find({age:{$ne:33}})

select * from users where name like "%Joe%"

db.users.find({name:/Joe/})

select * from users where name LIKE "Joe%"

db.users.find({name:/^Joe/})

select * from users where age>33 and age<=40

db.users.find({'age':{$gt:33,$lte:40}})

select * from users order by name desc

db.users.find().sort({name:-1})

select * from users where a=1 and b='q'

db.users.find({a:1,b:'q'})

select * from users limit 10 skip 20

db.users.find().limit(10).skip(20)

select * from users where a=1 or b=2

db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )

select * from users limit 1

db.users.findOne()

select order_id from orders o, order_line_items li where li.order_id=o.order_id and li.sku=12345

db.orders.find({"items.sku":12345},{_id:1})

select customer.name from customers,orders where orders.id="q179" and orders.custid=customer.id

var o = db.orders.findOne({_id:"q179"});

var name = db.customers.findOne({_id:o.custid})

 

 

select distinct last_name from users

db.users.distinct('last_name')

select count(*y)

from users

db.users.count()

select count(*y)

from users where age > 30

db.users.find({age: {'$gt': 30}}).count()

select count(age) from users

db.users.find({age: {'$exists': true}}).count()

 

 

create index myindexname on users(name)

db.users.ensureIndex({name:1})

create index myindexname ON users(name,ts desc)

db.users.ensureIndex({name:1,ts:-1})

 

 

explain select * from users where z=3

db.users.find({z:3}).explain()

 

 

update users set a=1 where b='q'

db.users.update({b:'q'}, {$set:{a:1}}, false, true)

update users set a=a+2 where b='q'

db.users.update({b:'q'}, {$inc:{a:2}}, false, true)

 

 

delete from users where z="abc"

db.users.remove({z:'abc'});

 

更多请见: http://api.mongodb.org/wiki/current/SQL%20to%20Aggregation%20Framework%20Mapping%20Chart.html

这些都是常用的SQL语句,当然掌握这些就足够后面的开发啦。

其实这些都是官方文档,我只是把资料按照自己的学习路线整理一下,对于一些复杂的概念我会做详细解释。如果有需要教程的话,可以发我邮箱229661693@qq.com,我整理的离线教程。

 

posted on 2012-12-18 00:44  Arts&Crafts  阅读(2606)  评论(1编辑  收藏  举报

导航