1、查询所有记录
db.userInfo.find();
相当于:select * from userInfo;
默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”
但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize = 50;这样每页就显示50条记录了。
2、查询去掉后的当前聚集集合中的某列的重复数据
db.userInfo.distinct(“name”);
会过滤掉name中的相同数据
相当于:select distict name from userInfo;
3、查询age = 22的记录
db.userInfo.find({“age”: 22});
相当于:select * from userInfo where age = 22;
4、查询age > 22的记录
db.userInfo.find({age: {$gt: 22}});
相当于:select * from userInfo where age > 22;
5、查询age < 22的记录
db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age < 22;
6、查询age >= 25的记录
db.userInfo.find({age: {$gte: 25}});
相当于:select * from userInfo where age >= 25;
7、查询age <= 25的记录
db.userInfo.find({age: {$lte: 25}});
8、查询age >= 23 并且age <= 26
db.userInfo.find({age: {$gte: 23, $lte: 26}});
9、查询name中包含mongo的数据
db.userInfo.find({name: /mongo/});
//相当于%%
select * from userInfo where name like ‘%mongo%’;
10、查询name中以mongo开头的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%’;
11、查询指定列name、age数据
db.userInfo.find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;
当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。
12、查询指定列name、age数据, age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from userInfo where age > 25;
13、按照年龄排序
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});
14、查询name = zhangsan, age = 22的数据
db.userInfo.find({name: ‘zhangsan’, age: 22});
相当于:select * from userInfo where name = ‘zhangsan’and age = ‘22’;
15、查询前5条数据
db.userInfo.find().limit(5);
相当于:select top 5 * from userInfo;
16、查询10条以后的数据
db.userInfo.find().skip(10);
相当于:select * from userInfo where id not in (
select top 10 * from userInfo
);
17、查询在5-10之间的数据
db.userInfo.find().limit(10).skip(5);
可用于分页,limit是pageSize,skip是第几页*pageSize
18、or与 查询
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
19、查询第一条数据
db.userInfo.findOne();
相当于:select top 1 * from userInfo;
db.userInfo.find().limit(1);
20、查询某个结果集的记录条数
db.userInfo.find({age: {$gte: 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
21、按照某列进行排序
db.userInfo.find({sex: {$exists: true}}).count();
相当于:select count(sex) from userInfo
下面我顺便看看两者的命令对比吧。
MySQL |
MongoDB |
说明 |
mysqld |
mongod |
服务器守护进程 |
mysql |
mongo |
客户端工具 |
mysqldump |
mongodump |
逻辑备份工具 |
mysql |
mongorestore |
逻辑恢复工具 |
db.repairDatabase() |
修复数据库 |
|
mysqldump |
mongoexport |
数据导出工具 |
source |
mongoimport |
数据导入工具 |
grant * privileges on *.* to … |
Db.addUser() Db.auth() |
新建用户并权限 |
show databases |
show dbs |
显示库列表 |
Show tables |
Show collections |
显示表列表 |
Show slave status |
Rs.status |
查询主从状态 |
Create table users(a int, b int) |
db.createCollection("mycoll", {capped:true, size:100000}) 另:可隐式创建表。 |
创建表 |
Create INDEX idxname ON users(name) |
db.users.ensureIndex({name:1}) |
创建索引 |
Create INDEX idxname ON users(name,ts DESC) |
db.users.ensureIndex({name:1,ts:-1}) |
创建索引 |
Insert into users values(1, 1) |
db.users.insert({a:1, b:1}) |
插入记录 |
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 |
db.users.find({'age':{$lt:33}}) |
条件查询 |
select * from users where age>33 and age<=40 |
db.users.find({'age':{$gt:33,$lte:40}}) |
条件查询 |
select * from users where a=1 and b='q' |
db.users.find({a:1,b:'q'}) |
条件查询 |
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 * from users where name like "%Joe%" |
db.users.find({name:/Joe/}) |
模糊查询 |
select * from users where name like "Joe%" |
db.users.find({name:/^Joe/}) |
模糊查询 |
select count(1) from users |
Db.users.count() |
获取表记录数 |
select count(1) from users where age>30 |
db.users.find({age: {'$gt': 30}}).count() |
获取表记录数 |
select DISTINCT last_name from users |
db.users.distinct('last_name') |
去掉重复值 |
select * from users ORDER BY name |
db.users.find().sort({name:-1}) |
排序 |
select * from users ORDER BY name DESC |
db.users.find().sort({name:-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'}) |
删除记录 |
db. users.remove() |
删除所有的记录 |
|
drop database IF EXISTS test; |
use test db.dropDatabase() |
删除数据库 |
drop table IF EXISTS test; |
db.mytable.drop() |
删除表/collection |
db.addUser(‘test', 'test') |
添加用户 readOnly-->false |
|
db.addUser(‘test', 'test', true) |
添加用户 readOnly-->true |
|
db.addUser("test","test222") |
更改密码 |
|
db.system.users.remove({user:"test"}) 或者db.removeUser('test') |
删除用户 |
|
use admin |
超级用户 |
|
db.auth(‘test', ‘test') |
用户授权 |
|
db.system.users.find() |
查看用户列表 |
|
show users |
查看所有用户 |
|
db.printCollectionStats() |
查看各collection的状态 |
|
db.printReplicationInfo() |
查看主从复制状态 |
|
show profile |
查看profiling |
|
db.copyDatabase('mail_addr','mail_addr_tmp') |
拷贝数据库 |
|
db.users.dataSize() |
查看collection数据的大小 |
|
db. users.totalIndexSize() |
查询索引的大小 |