首先介绍一些关系数据库中常用的概念对比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语句,当然掌握这些就足够后面的开发啦。
CREATE TABLE USERS (a Number, b Number) | Implicit or use MongoDB::createCollection(). |
INSERT INTO USERS VALUES(1,1) | $db->users->insert(array("a" => 1, "b" => 1)); |
SELECT a,b FROM users | $db->users->find(array(), array("a" => 1, "b" => 1)); |
SELECT * FROM users WHERE age=33 | $db->users->find(array("age" => 33)); |
SELECT a,b FROM users WHERE age=33 | $db->users->find(array("age" => 33), array("a" => 1, "b" => 1)); |
SELECT a,b FROM users WHERE age=33 ORDER BY name | $db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1)); |
SELECT * FROM users WHERE age>33 | $db->users->find(array("age" => array('$gt' => 33))); |
SELECT * FROM users WHERE age<33 | $db->users->find(array("age" => array('$lt' => 33))); |
SELECT * FROM users WHERE name LIKE "%Joe%" | $db->users->find(array("name" => new MongoRegex("/Joe/"))); |
SELECT * FROM users WHERE name LIKE "Joe%" | $db->users->find(array("name" => new MongoRegex("/^Joe/"))); |
SELECT * FROM users WHERE age>33 AND age<=40 | $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40))); |
SELECT * FROM users ORDER BY name DESC | $db->users->find()->sort(array("name" => -1)); |
CREATE INDEX myindexname ON users(name) | $db->users->ensureIndex(array("name" => 1)); |
CREATE INDEX myindexname ON users(name,ts DESC) | $db->users->ensureIndex(array("name" => 1, "ts" => -1)); |
SELECT * FROM users WHERE a=1 and b='q' | $db->users->find(array("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(array('$or' => array(array("a" => 1), array("b" => 2)))); |
SELECT * FROM users LIMIT 1 | $db->users->find()->limit(1); |
EXPLAIN SELECT * FROM users WHERE z=3 | $db->users->find(array("z" => 3))->explain() |
SELECT DISTINCT last_name FROM users | $db->command(array("distinct" => "users", "key" => "last_name")); |
SELECT COUNT(*y) FROM users | $db->users->count(); |
SELECT COUNT(*y) FROM users where AGE > 30 | $db->users->find(array("age" => array('$gt' => 30)))->count(); |
SELECT COUNT(AGE) from users | $db->users->find(array("age" => array('$exists' => true)))->count(); |
UPDATE users SET a=1 WHERE b='q' | $db->users->update(array("b" => "q"), array('$set' => array("a" => 1))); |
UPDATE users SET a=a+2 WHERE b='q' | $db->users->update(array("b" => "q"), array('$inc' => array("a" => 2))); |
DELETE FROM users WHERE z="abc" | $db->users->remove(array("z" => "abc")); |