WD1X.COM - 问答一下,轻松解决,电脑应用解决专家
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win11Linux
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascript服务器
PHP教程CSS教程XML教程

MongoBD命令大全

更新时间:2021-03-01 09:30 作者:Tonny点击:
一、MongoDB简介
 
简单介绍一下MongoDB:
 
传统的关系数据库(如mysql)一般由数据库(database)、表(table)、记录(record)三个层次概念组成,
MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。
MongoDB稳定
MongoDB支持索引,索引放在内存中,能够提升随机读写的性能。如果索引不能完全放在内存,一旦出现随机读写比较高的时候,就会频繁地进行磁盘交换,MongoDB的性能就会急剧下降
MongoDB占用的空间很大,因为它属于典型空间换时间原则的类型。那么它的磁盘空间比普通数据库会浪费一些
 
二、MongoDB数据库操作
 
查看mongo帮助文档
 
mongo -h
 
2、登陆
 
mongo -umiaobt -p --authenticationDatabase
mongo -umiaobt -p331sharp --authenticationDatabase
 
(以下操作需要登陆数据库才能操作)
 
// 库操作
3、查询所有库
 
show dbs;  
 
4、查询库中的连接
 
show collecitons;
 
5、创建数据库/切换数据库
 
use test1;
 
如果没有test1这个库,会创建
 
6、删除数据库
 
db.dropDatabase();
 
7、获取数据库名称
 
db.getName();
 
8、获取数据库状态
 
db.stats();
 
9、当前db版本
 
db.version();  
 
10、查看当前db的链接机器地址
 
db.getMongo();
 
11、从指定主机上克隆数据库
 
db.cloneDatabase("127.0.0.1"); 
 
12、从指定的机器上复制指定数据库数据到某个数据库
 
db.copyDatabase("yhb", "test1", "127.0.0.1");  
 
13、修复数据库
 
db.repairDatabase();  
 
在MongoDB中频繁的进行数据增删改时,如果记录变了,例如数据大小发生了变化,这时候容易产生一些数据碎片,出现碎片引发的结果,
一个是索引会出现性能问题,另外一个就是在一定的时间后,所占空间会莫明其妙地增大,所以要定期把数据库做修复,定期重新做索引,这样会提升MongoDB的稳定性和效率
 
三、MongoDB集合操作
 
1、创建一个聚集集合(table)
 
//指定数据库大小size,最大存放100个文档,满了,就会mongodb 会删除旧文档。
db.createCollection("human",{"size":1024,capped:true,max:100});
db.createCollection("people");
 
2、查看集合状态
 
db.people.stats();
 
3、获取指定集合
 
db.getCollection("human");  
 
4、获取当前db中的所有集合
 
db.getCollectionNames(); 
 
和show collections类似
 
5、显示当前db所有聚集索引的状态
 
db.printCollectionStats();  
 
四、MongoBD用户操作
 
1、添加一个用户
 
db.createUser({user:"zs",pwd:"111",roles:["read"]})
 
添加用户、设置密码、是否只读
 
2、数据库认证、安全模式
db.auth(“zs”, “111”);
 
3、显示当前所有用户,角色
 
show people;
show roles;
 
4、删除用户
 
db.removeUser("zs");  
 
五、聚集集合查询
 
1、查询所有记录
 
引用块内容
 
db.people.find();  
相当于:select* from people;
 
2、查询去掉后的当前聚集集合中的某列的重复数据
 
db.people.distinct("name");  
相当于:select distict name from people;
 
3、查询age = 18的记录
 
db.people.find({"age": 18});  
相当于: select * from people where age = 18;
 
4、查询age > 18的记录
 
db.people.find({age: {$gt: 18}});  
相当于:select * from people where age >18;
 
5、查询age < 18的记录
 
db.people.find({age: {$lt: 18}});  
相当于:select * from people where age <18;
 
6、查询age >= 18的记录
 
db.people.find({age: {$gte: 18}});  
相当于:select * from people where age >= 18;
 
7、查询age <= 18的记录
 
db.people.find({age: {$lte: 18}}); 
 
8、查询age >= 23 并且 age <= 26
 
db.people.find({age: {$gte: 23, $lte: 26}});  
 
9、查询name中包含 mongo的数据
 
db.people.find({name: /mongo/});  
相当于:select * from people where name like '%mongo%';
 
10、查询name中以mongo开头的
 
db.people.find({name: /^mongo/});  
 
相当于:select * from people where name like ‘mongo%’;
 
11、查询指定列name、age数据
 
db.people.find({}, {name: 1, age: 1});  
 
相当于:select name, age from people;
当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。
 
12、查询指定列name、age数据, age > 18
 
db.people.find({age: {$gt: 18}}, {name: 1, age: 1});  
相当于:select name, age from people where age >18;
 
13、按照年龄排序
 
升序:db.people.find().sort({age: 1});  
降序:db.people.find().sort({age: -1});  
 
14、查询name = zhangsan, age = 18的数据
 
db.people.find({name: 'zhangsan', age: 18});  
相当于:select * from people where name = 'zhangsan' and age = '18';
 
15、查询前5条数据
 
db.people.find().limit(5);  
相当于:select * from people Limit 5;
 
16、查询10条以后的数据
 
db.people.find().skip(10);  
相当于:select * from people where id not in (select id from people limit 10);
 
17、查询在5-10之间的数据
 
db.people.find().limit(10).skip(5);  
可用于分页,limit是pageSize,skip是第几页*pageSize
 
18、or与查询
 
db.people.find({$or: [{age: 18}, {age: 18}]});  
相当于:select * from people where age = 18 or age = 18;
 
19、查询第一条数据
 
db.people.findOne();  
相当于:select * from people limit 1;
db.people.find().limit(1);
 
20、查询某个结果集的记录条数
 
db.people.find({age: {$gte: 18}}).count();  
相当于:select count(*) from people where age >= 20;
 
21、求总数
 
db.people.find({sex: {$exists: true}}).count();
相当于:select count(sex) from people;
 
六、集合的索引
 
1、创建索引
 
db.people.ensureIndex({name: 1});  
db.people.ensureIndex({name: 1, ts: -1});  //联合索引
db.people.ensureIndex({"name":1},{"unique":true}); //唯一索引
 
2、查询当前聚集集合所有索引
 
db.people.getIndexes();  
 
3、查看总索引记录大小
 
db.people.totalIndexSize();  
 
4、读取当前集合的所有index信息
 
db.people.reIndex();  
 
5、删除指定索引
 
db.people.dropIndex("name_1");
 
6、删除所有索引索引
 
db.people.dropIndexes();  
 
七、修改、添加、删除集合数据
 
1、添加
 
db.people.save({name: 'zhangsan', age: 18, sex: true});  
 
添加的数据的数据列,没有固定,根据添加的数据为准
 
2、修改
 
db.people.update({age: 18}, {$set: {name: 'changeName'}}, false, true);  
相当于:update people set name = 'changeName' where age = 18;  
db.people.update({name: 'zhangs'}, {$inc: {age: 12}}, false, true);  
相当于:update people set age = age + 12 where name = 'zhangs';  
db.people.update({name: 'zhangs'}, {$inc: {age: 12}, $set: {name: 'hoho'}}, false, true);  
相当于:update people set age = age + 12, name = 'hoho' where name = 'zhangs';  
 
3、删除
 
db.people.remove({age: 12});  
 
其他命令:
.pretty() 将结果集格式化
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容