文章目录
- 一、常用命令:
- 二、聚集集合查询:
- 1、查询所有记录
- 2、查询去掉后的当前聚集集合中的某列的重复数据
- 3、查询age = 22的记录
- 4、查询age不等于22的记录
- 5、查询age > 22的记录
- 6、查询age < 22的记录
- 7、查询age >= 25的记录
- 8、查询age <= 25的记录
- 9、查询age >= 23 并且 age <= 26
- 10、查询name中包含 mongo的数据
- 11、查询name中以mongo开头的
- 12、查询指定列name、age数据
- 13、查询指定列name、age数据, age > 25
- 14、按照年龄排序
- 15、查询某个字段的最大值
- 16、查询name = zhangsan, age = 22的数据
- 17、查询前5条数据
- 18、查询10条以后的数据
- 19、查询在5-10之间的数据
- 20、or 查询
- 21、查询第一条数据
- 22、查询某个结果集的记录条数
- 23、按_id查询一条记录
- 24、count查询记录条数
- 25、查询某个字段存在的数据
- 三、索引:
- 四、修改、添加、删除集合数据
- 五、其他
- 六、所遇到的坑
一、常用命令:
1、登录命令行(40008为自定义的端口)
mongo --port 40008
2、删除当前使用数据库
db.dropDatabase();
3、删除集合(相当于mysql中的表)
db.COLLECTION_NAME.drop()
4、查看当前使用的数据库
db.getName();
5、显示当前db状态
db.stats();
6、当前db版本
db.version();
7、查看当前db的链接机器地址
db.getMongo();
8、显示当前所有用户
show users
9、同一个主机上从一个db的表复制到另一个db的表
db.collection_name.find().forEach(function(d){ db.getSiblingDB('new_database')['collection_name'].insert(d);})
注:在同一主机上mongoDB可以直接复制数据库,但是对于数据库里的表却没有直接的复制语句。虽然用上面遍历数据再插入的方式可以实现,但是效率很低而且数据量多的时候并不能全部复制过去,比如数据库A中的test表有600条数据,执行完上面的语句后发现复制到数据库B中的test表只有200多条数据。
10、不同主机上复制表
db.runCommand({cloneCollection:<namespace>, fromhost:<hostname>, query:<filter>});
例:
db.runCommand({cloneCollection:'testdb.testcol', from:'192.168.1.12:27017',copyIndexes:false, query:{'age':{'gt':2}}});
11、复制数据库
db.copyDatabase(fromdb,todb,fromhost,username,password,mechanism)
后面四个选项可选:
- fromdbt: 源db;
- todb: 目标db;
- fromhost: 源db的主机地址,如果在同一个mongod实例内可以省略;
- username: 如果开启了验证模式,需要源DB主机上的MongoDB实例的用户名;
- password: 同上,需要对应用户的密码;
- mechanism: fromhost验证username和password的机制,有:MONGODB-CR、SCRAM-SHA-1两种。
例:
db.copyDatabase('test','test2','192.168.14.52:27017','test','test','SCRAM-SHA-1')
二、聚集集合查询:
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;或者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: {$ne: 22}});
5、查询age > 22的记录
db.userInfo.find({age: {$gt: 22}});
相当于:select * from userInfo where age >22;
6、查询age < 22的记录
db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age <22;
7、查询age >= 25的记录
db.userInfo.find({age: {$gte: 25}});
相当于:select * from userInfo where age >= 25;
8、查询age <= 25的记录
db.userInfo.find({age: {$lte: 25}});
9、查询age >= 23 并且 age <= 26
db.userInfo.find({age: {$gte: 23, $lte: 26}});
10、查询name中包含 mongo的数据
db.userInfo.find({name: /mongo/});
//相当于%%
select * from userInfo where name like ‘%mongo%';
11、查询name中以mongo开头的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%';
12、查询指定列name、age数据
db.userInfo.find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;
注:当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。
13、查询指定列name、age数据, age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from userInfo where age >25;
14、按照年龄排序
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});
15、查询某个字段的最大值
最小值:db.userInfo.find().sort({age: 1})limit(1);
最大值:db.userInfo.find().sort({age: -1})limit(1);
Springboot:
Query query_max = new Query();
Criteria criteria_max = new Criteria();
criteria_max.and("subjectId").is(subjectId);
query_max.addCriteria(criteria_max);
query_max.with(new Sort(new Order(Direction.DESC,"mediaCount"))).limit(1);
SpreadAggregationXj maxObject = mongoTemplate.findOne(query_max, SpreadAggregationXj.class);
map.put("maxnewCount", maxObject.getMediaCount());
16、查询name = zhangsan, age = 22的数据
db.userInfo.find({name: 'zhangsan', age: 22});
相当于:select * from userInfo where name = ‘zhangsan' and age = ‘22';
17、查询前5条数据
db.userInfo.find().limit(5);
相当于:select top 5 * from userInfo;
18、查询10条以后的数据
db.userInfo.find().skip(10);
相当于:select * from userInfo where id not in (
select top 10 * from userInfo);
19、查询在5-10之间的数据
db.userInfo.find().limit(10).skip(5);
注:可用于分页,limit是pageSize,skip是第几页*pageSize
20、or 查询
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
21、查询第一条数据
db.userInfo.findOne();
相当于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);
22、查询某个结果集的记录条数
db.userInfo.find({age: {$gte: 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
23、按_id查询一条记录
db.getCollection('comments').find({"_id":ObjectId("5b179684e4b01fcf1c11c7d4")})
相当于:select * from userInfo where id = ....;
24、count查询记录条数
db.monitor.find().count()
当使用limit()方法限制返回的记录数时,默认情况下count()方法仍然返回全部记录条数。 例如,下面的示例中返回的不是5,而是monitor表中所有的记录数量:
db.monitor.find().skip(10).limit(5).count();
如果希望返回限制之后的记录数量,要使用count(true)或者count(非0):
db.monitor.find().skip(10).limit(5).count(true);
25、查询某个字段存在的数据
db.getCollection('monitor').find({warningThreshold:{$exists:true}})
注:在mongodb中,如果实体类的某个字段(为string类型)为null,则在存入mongodb时该字段不会存入,如果是long类型,则存入为NumberLong(0);
例:
System.out.println(findHui.getUserName());为null
System.out.println(findHui.getAttUnix());为0
三、索引:
1、创建索引
db.userInfo.ensureIndex({name: 1});
db.userInfo.ensureIndex({name: 1, ts: -1});
2、查询当前聚集集合所有索引
db.userInfo.getIndexes();
3、查看总索引记录大小
db.userInfo.totalIndexSize();
4、读取当前集合的所有index信息
db.users.reIndex();
5、删除指定索引
db.users.dropIndex("name_1");
6、删除所有索引索引
db.users.dropIndexes();
四、修改、添加、删除集合数据
1、添加
db.users.save({name: 'zhangsan', age: 25, sex: true});
2、修改
db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);
相当于:update users set name = 'changeName' where age = 25;
db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);
相当于:update users set age = age + 50 where name = 'Lisi';
db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);
相当于:update users set age = age + 50, name = 'hoho' where name = 'Lisi';
3、删除一条数据
db.users.remove({age: 132});
4、清空表数据
db.users.drop();
五、其他
1、循环添加数据
> for (var i = 0; i < 30; i++) {
... db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
... };
这样就循环添加了30条数据,同样也可以省略括号的写法
> for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
2、查询之前的错误信息
db.getPrevError();
3、清除错误记录
db.resetError();
六、所遇到的坑
今天遇到了这么个问题,就是根据相关字段查询特别慢,有时候直接就报java.io.IOException了
db.getCollection('spreadAggregationXj').find({"aggType" : "day" , "subjectId" : "5c7c8131f241255e462fc3a9" , "isAll" : "0" , "mediaId" : "1"})
解决:在表上加相应字段的索引
困惑:有的表没这些索引也不慢啊,为什么这个表就不行,是不是还有哪块的查询逻辑我没有发现呐