MongoDB命令大全

    一、MongoDB简介

简单介绍一下MongoDB:
传统的关系数据库(如mysql)一般由数据库(database)、表(table)、记录(record)三个层次概念组成,
MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。
MongoDB稳定
MongoDB支持索引,索引放在内存中,能够提升随机读写的性能。如果索引不能完全放在内存,一旦出现随机读写比较高的时候,就会频繁地进行磁盘交换,MongoDB的性能就会急剧下降
MongoDB占用的空间很大,因为它属于典型空间换时间原则的类型。那么它的磁盘空间比普通数据库会浪费一些

    二、MongoDB数据库操作

查看mongo帮助文档

mongo -h

    1

2、登陆

mongo -umiaobt -p --authenticationDatabase
mongo -umiaobt -p331sharp --authenticationDatabase

    1
    2

(以下操作需要登陆数据库才能操作)

// 库操作
3、查询所有库

show dbs;  

    1

4、查询库中的连接

show collecitons;

    1

5、创建数据库/切换数据库

use test1;

    1

如果没有test1这个库,会创建

6、删除数据库

db.dropDatabase();

    1

7、获取数据库名称

db.getName();

    1

8、获取数据库状态

db.stats();

    1

9、当前db版本

db.version();  

    1

10、查看当前db的链接机器地址

db.getMongo();

    1

11、从指定主机上克隆数据库

db.cloneDatabase("127.0.0.1");

    1

12、从指定的机器上复制指定数据库数据到某个数据库

db.copyDatabase("yhb", "test1", "127.0.0.1");  

    1

13、修复数据库

db.repairDatabase();  

    1

在MongoDB中频繁的进行数据增删改时,如果记录变了,例如数据大小发生了变化,这时候容易产生一些数据碎片,出现碎片引发的结果,
一个是索引会出现性能问题,另外一个就是在一定的时间后,所占空间会莫明其妙地增大,所以要定期把数据库做修复,定期重新做索引,这样会提升MongoDB的稳定性和效率

    三、MongoDB集合操作

1、创建一个聚集集合(table)

//指定数据库大小size,最大存放100个文档,满了,就会mongodb 会删除旧文档。
db.createCollection("human",{"size":1024,capped:true,max:100});
db.createCollection("people");

    1
    2
    3

2、查看集合状态

db.people.stats();

    1

3、获取指定集合

db.getCollection("human");  

    1

4、获取当前db中的所有集合

db.getCollectionNames();

    1

和show collections类似

5、显示当前db所有聚集索引的状态

db.printCollectionStats();  

    1

    四、MongoBD用户操作

1、添加一个用户

db.createUser({user:"zs",pwd:"111",roles:["read"]})

    1

添加用户、设置密码、是否只读

2、数据库认证、安全模式
db.auth(“zs”, “111”);

3、显示当前所有用户,角色

show people;
show roles;

4、删除用户

db.removeUser("zs");  

    1

五、聚集集合查询

1、查询所有记录

    引用块内容

db.people.find();  
相当于:select* from people;

    1
    2

2、查询去掉后的当前聚集集合中的某列的重复数据

db.people.distinct("name");  
相当于:select distict name from people;

    1
    2

3、查询age = 18的记录

db.people.find({"age": 18});  
相当于: select * from people where age = 18;

    1
    2

4、查询age > 18的记录

db.people.find({age: {$gt: 18}});  
相当于:select * from people where age >18;

    1
    2

5、查询age < 18的记录

db.people.find({age: {$lt: 18}});  
相当于:select * from people where age <18;

    1
    2

6、查询age >= 18的记录

db.people.find({age: {$gte: 18}});  
相当于:select * from people where age >= 18;

    1
    2

7、查询age <= 18的记录

db.people.find({age: {$lte: 18}});

    1

8、查询age >= 23 并且 age <= 26

db.people.find({age: {$gte: 23, $lte: 26}});  

    1

9、查询name中包含 mongo的数据

db.people.find({name: /mongo/});  
相当于:select * from people where name like '%mongo%';

    1
    2

10、查询name中以mongo开头的

db.people.find({name: /^mongo/});  

    1

相当于:select * from people where name like ‘mongo%’;

11、查询指定列name、age数据

db.people.find({}, {name: 1, age: 1});  

    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;

    1
    2

13、按照年龄排序

升序:db.people.find().sort({age: 1});  
降序:db.people.find().sort({age: -1});  

    1
    2

14、查询name = zhangsan, age = 18的数据

db.people.find({name: 'zhangsan', age: 18});  
相当于:select * from people where name = 'zhangsan' and age = '18';

    1
    2

15、查询前5条数据

db.people.find().limit(5);  
相当于:select * from people Limit 5;

    1
    2

16、查询10条以后的数据

db.people.find().skip(10);  
相当于:select * from people where id not in (select id from people limit 10);

    1
    2

17、查询在5-10之间的数据

db.people.find().limit(10).skip(5);  
可用于分页,limit是pageSize,skip是第几页*pageSize

    1
    2

18、or与查询

db.people.find({$or: [{age: 18}, {age: 18}]});  
相当于:select * from people where age = 18 or age = 18;

    1
    2

19、查询第一条数据

db.people.findOne();  
相当于:select * from people limit 1;
db.people.find().limit(1);

    1
    2
    3

20、查询某个结果集的记录条数

db.people.find({age: {$gte: 18}}).count();  
相当于:select count(*) from people where age >= 20;

    1
    2

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}); //唯一索引

    1
    2
    3

2、查询当前聚集集合所有索引

db.people.getIndexes();  

    1

3、查看总索引记录大小

db.people.totalIndexSize();  

    1

4、读取当前集合的所有index信息

db.people.reIndex();  

    1

5、删除指定索引

db.people.dropIndex("name_1");

    1

6、删除所有索引索引

db.people.dropIndexes();  

    1

    七、修改、添加、删除集合数据

1、添加

db.people.save({name: 'zhangsan', age: 18, sex: true});  

    1

添加的数据的数据列,没有固定,根据添加的数据为准

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';  

    1
    2
    3
    4
    5
    6

3、删除

db.people.remove({age: 12});
---------------------
作者:Tonny__
来源:CSDN
原文:https://blog.csdn.net/zc375039901/article/details/79951047
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 10
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值