本文共 1702 字,大约阅读时间需要 5 分钟。
show dbs:显示数据库列表
show collections:显示当前数据库中表的集合(类似于关系数据中的表)
show users:显示当前数据库用户
use <dbname>:切换数据库
db.help() :数据库管理命令
db.foo.help():显示集合操作命令
db.dropDatabase():删除当前使用的数据库
db.getName():显示当前使用的数据库
db.stats():显示当前数据库的状态
db.version():显示数据库版本
db.getMongo():查看当前数据库的ip地址
添加用户:db.addUser("root","password");
数据库认证:db.auth("root","password")
密码修改:db.addUser("root","pass",true) 最后一个参数readOnly,读写权限设置
查询:
1、创建集合:
db.createCollections('user', { max: 100, size: 10000, capped: true})
2、插入记录
db.user.insert({ name: "tom", age: 26, status: "A", groups: ["news", "sports"]});
3、查询全部
db.user.find()
4、limt(5)查询前5条:
db.user.find({ age: { $gt: 30 }}, { name: 1, groups: 1}).limit(5);相当于select id,name,groups form userwhere age>30limit(5)
5、skip(10)查询10条以后的记录
db.user.find().skip(10);相当于:select * from user where id not in(select top 10 from user)
6、sort(age)按照年龄排序
升序:db.user.find().sort(age:1)降序:db.user.find().sort(age:-1)
7、查询在5-10之间的数据
db.user.find().limit(10).skip(5);可用于分页,limit是pageSize,skip是第几页*pageSize
8、查询去掉后的当前聚集集合中的某列的重复数据
db.user.distinct("name");会过滤掉name中的相同数据相当于:select distict name from usero;9、查询age = 22的记录 db.user.find({"age": 22}); 相当于: select * from user where age = 22; 10、查询age > 22的记录 db.user.find({age: {$gt: 22}}); 相当于:select * from user where age >22; 11、查询age < 22的记录 db.user.find({age: {$lt: 22}}); 相当于:select * from user where age <22; 12、查询age >= 25的记录 db.user.find({age: {$gte: 25}}); 相当于:select * from user where age >= 25; 13、查询age <= 25的记录 db.user.find({age: {$lte: 25}}); 14、查询age >= 23 并且 age <= 26 db.user.find({age: {$gte: 23, $lte: 26}}); 15、查询name中包含 mongo的数据 db.user.find({name: /mongo/}); //相当于%% select * from user where name like ‘%mongo%’;
转载地址:http://dirji.baihongyu.com/