时间:2021-07-01 10:21:17 帮助过:4人阅读
一、打开命令行
cmd --> cd C:\Program Files\MongoDB\Server\3.0\bin\ --> mongo.exe 二、连接远程机器命令行工具 1、连接远程服务器(注意该命令是定位到bin目录下执行,而不是定位到mongo.exe下执行) mongo.exe 192.168.1.27:27017 2、使用数据库 use DBName三、常用命令行(管理)
四、常用命令行(集合)
- <span style="line-height: 1.5; font-size: 12px; color: #333300;"> </span>
- <strong style="color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19.5px; white-space: normal; font-size: 14px;">五、常用命令行(索引)</strong>
- db<span style="line-height: 1.5; color: #cc6633;">.userInfo</span><span style="line-height: 1.5; color: #cc6633;">.ensureIndex</span>({name: 1});
- db<span style="line-height: 1.5; color: #cc6633;">.userInfo</span><span style="line-height: 1.5; color: #cc6633;">.ensureIndex</span>({name: 1, ts: -1}); --联合索引
- db<span style="line-height: 1.5; color: #cc6633;">.userInfo</span><span style="line-height: 1.5; color: #cc6633;">.getIndexes</span>();
- db<span style="line-height: 1.5; color: #cc6633;">.userInfo</span><span style="line-height: 1.5; color: #cc6633;">.totalIndexSize</span>();
- db<span style="line-height: 1.5; color: #cc6633;">.users</span><span style="line-height: 1.5; color: #cc6633;">.reIndex</span>();
- db<span style="line-height: 1.5; color: #cc6633;">.users</span><span style="line-height: 1.5; color: #cc6633;">.dropIndex</span>("
- name
- ");
- db<span style="line-height: 1.5; color: #cc6633;">.users</span><span style="line-height: 1.5; color: #cc6633;">.dropIndexes</span>();
六、常用命令行(查询)
- 相当于<span style="line-height: 1.5; color: #006080;">:select distict name from userInfo;</span>
- db<span style="line-height: 1.5; color: #cc6633;">.</span><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘, Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; line-height: 21px; white-space: normal; font-size: 10.5pt;"><collectionName></span><span style="font-family: 微软雅黑; line-height: 1.5; color: #cc6633;">.</span><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘, Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; line-height: 21px; white-space: normal; font-size: 10.5pt;"> find</span><span style="font-family: 微软雅黑;">({age: {$gte: 23, $lte: 26},name:‘james‘});</span>
- db<span style="line-height: 1.5; color: #cc6633;">.</span><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘, Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 10.5pt; line-height: 21px; white-space: normal;"><collectionName></span><span style="font-family: 微软雅黑; line-height: 1.5; color: #cc6633;">.</span><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘, Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 10.5pt; line-height: 21px; white-space: normal;"> find</span><span style="font-family: 微软雅黑;">({name: /mongo/}); <span style="font-size: 14px;">(相当于‘%mongo%‘)</span></span>
- db<span style="line-height: 1.5; color: #cc6633;">.</span><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘, Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 10.5pt; line-height: 21px; white-space: normal;"><collectionName></span><span style="font-family: 微软雅黑; line-height: 1.5; color: #cc6633;">.</span><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘, Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 10.5pt; line-height: 21px; white-space: normal;"> find</span><span style="font-family: 微软雅黑;">({}, {name: 1, age: 1});</span><span style="font-family: 微软雅黑; line-height: 1.5; color: #cc6633;">.</span><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘, Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 10.5pt; line-height: 21px; white-space: normal;"> find</span><span style="font-family: 微软雅黑;">({name: /^mongo/}); </span><span style="font-size: 14px; font-family: 微软雅黑;">(相当于‘mongo%‘)</span>
七、常用命令行(增删改)
- db<span style="line-height: 1.5; color: #cc6633;">.users</span><span style="line-height: 1.5; color: #cc6633;">.findAndModify</span>({
- query: {age: {$gte: 25}},
- sort: {age: -1},
- update: {$set: {name: ‘a2‘}, $inc: {age: 2}},
- remove: true
- });
db.COLLECTION_NAME.findAndModify({query:{},
update:{},
remove:true|false,
new:true|false,
sort:{},
fields:{},
upsert:true|false});
query是查询选择器,与findOne的查询选择器相同
update是要更新的值,不能与remove同时出现
remove表示删除符合query条件的文档,不能与update同时出现
new为true:返回个性后的文档,false:返回个性前的,默认是false
sort:排序条件,与sort函数的参数一致。
fields:投影操作,与find*的第二个参数一致。
upsert:与update的upsert参数一样
mongodb_命令行
标签: