作用 |
MySQL |
MongoDB |
|
|
|
服务器守护进程 |
mysqld |
mongod |
客户端工具 |
mysql |
mongo |
逻辑备份工具 |
mysqldump |
mongodump |
逻辑还原工具 |
mysql |
mongorestore |
数据导出工具 |
mysqldump |
mongoexport |
数据导入工具 |
source |
mongoimport |
|
|
|
新建用户并授权 |
grant all on *.* |
db.addUser("user","psw") |
显示库列表 |
show databases; |
show dbs |
进去库 |
use dbname; |
use dbname |
显示表列表 |
show tables; |
show collections |
查询主从状态 |
show slave status; |
rs.status |
创建库 |
create database name; |
无需单独创建,直接use进去 |
创建表 |
create table tname(id int); |
无需单独创建,直接插入数据 |
删除表 |
drop table tname; |
db.tname.drop() |
删除库 |
drop database dbname; |
首先进去该库,db.dropDatabase() |
|
|
|
插入记录 |
insert into tname(id) value(2); |
db.tname.insert({id:2}) |
删除记录 |
delete from tname where id=2; |
db.tname.remove({id:2}) |
修改/更新记录 |
update tname set id=3 |
db.tname.update({id:2}, |
|
|
|
查询所有记录 |
select * from tname; |
db.tname.find() |
查询所有列 |
select id from tname; |
db.tname.find({},{id:1}) |
条件查询 |
select * from tname where id=2; |
db.tname.find({id:2}) |
条件查询 |
select * from tname where id < 2; |
db.tname.find({id:{$lt:2}}) |
条件查询 |
select * from tname where id >=2; |
db.tname.find({id:{$gte:2}}) |
条件查询 |
select * from tname where id=2 |
db.tname.find({id:2, |
条件查询 |
select * from tname where id=2 |
db.tname.find($or:[{id:2}, |
条件查询 |
select * from tname limit 1; |
db.tname.findOne() |
|
|
|
模糊查询 |
select * from tname where name |
db.tname.find({name:/ste/}) |
模糊查询 |
select * from tname where name |
db.tname.find({name:/^ste/}) |
|
|
|
获取表记录数 |
select count(id) from tname; |
db.tname.count() |
获取有条件 |
select count(id) from tname |
db.tname.find({id:2}).count() |
查询时去掉 |
select distinct(last_name) |
db.tname.distinct('last_name') |
|
|
|
正排序查询 |
select *from tname order by id; |
db.tname.find().sort({id:1}) |
逆排序查询 |
select *from tname |
db.tname.find().sort({id:-1}) |
|
|
|
取存储路径 |
explain select * from tname |
db.tname.find({id=3}).explain() |