2023年7月26日发(作者:)
MongoDB客户端命令总结⽂章⽬录⼀、常⽤命令:1、登录命令⾏(40008为⾃定义的端⼝)mongo --port 400082、删除当前使⽤数据库tabase();3、删除集合(相当于mysql中的表)TION_()4、查看当前使⽤的数据库e();5、显⽰当前db状态();6、当前db版本n();7、查看当前db的链接机器地址go();8、显⽰当前所有⽤户show users9、同⼀个主机上从⼀个db的表复制到另⼀个db的表tion_().forEach(function(d){ lingDB('new_database')['collection_name'].insert(d);})注:在同⼀主机上mongoDB可以直接复制数据库,但是对于数据库⾥的表却没有直接的复制语句。虽然⽤上⾯遍历数据再插⼊的⽅式可以实现,但是效率很低⽽且数据量多的时候并不能全部复制过去,⽐如数据库A中的test表有600条数据,执⾏完上⾯的语句后发现复制到数据库B中的test表只有200多条数据。10、不同主机上复制表mand({cloneCollection:
⼆、聚集集合查询:1、查询所有记录();相当于:select* from userInfo;注:默认每页显⽰20条记录,当显⽰不下的情况下,可以⽤it迭代命令查询下⼀页数据。键⼊it命令不能带“;”,但是你可以设置每页显⽰数据的⼤⼩,⽤atchSize= 50;这样每页就显⽰50条记录了。2、查询去掉后的当前聚集集合中的某列的重复数据ct("name");会过滤掉name中的相同数据相当于:select distict name from userInfo;或者select distict(name) from userInfo;数据结构:3、查询age = 22的记录({"age": 22});相当于: select * from userInfo where age = 22;4、查询age不等于22的记录({age: {$ne: 22}});5、查询age > 22的记录({age: {$gt: 22}});相当于:select * from userInfo where age >22;6、查询age < 22的记录({age: {$lt: 22}});相当于:select * from userInfo where age <22;7、查询age >= 25的记录({age: {$gte: 25}});相当于:select * from userInfo where age >= 25;8、查询age <= 25的记录({age: {$lte: 25}});9、查询age >= 23 并且 age <= ({age: {$gte: 23, $lte: 26}});10、查询name中包含 mongo的数据({name: /mongo/});//相当于%%select * from userInfo where name like ‘%mongo%';11、查询name中以mongo开头的({name: /^mongo/});select * from userInfo where name like ‘mongo%';12、查询指定列name、age数据({}, {name: 1, age: 1});相当于:select name, age from userInfo;注:当然name也可以⽤true或false,当⽤ture的情况下河name:1效果⼀样,如果⽤false就是排除name,显⽰name以外的列信息。13、查询指定列name、age数据, age > ({age: {$gt: 25}}, {name: 1, age: 1});相当于:select name, age from userInfo where age >25;14、按照年龄排序升序:().sort({age: 1});降序:().sort({age: -1});15、查询某个字段的最⼤值最⼩值:().sort({age: 1})limit(1);最⼤值:().sort({age: -1})limit(1);Springboot:Query query_max = new Query();Criteria criteria_max = new Criteria();criteria_("subjectId").is(subjectId);query_teria(criteria_max);query_(new Sort(new Order(,"mediaCount"))).limit(1);SpreadAggregationXj maxObject = e(query_max, );("maxnewCount", iaCount());16、查询name = zhangsan, age = 22的数据({name: 'zhangsan', age: 22});相当于:select * from userInfo where name = ‘zhangsan' and age = ‘22';17、查询前5条数据().limit(5);相当于:select top 5 * from userInfo;18、查询10条以后的数据().skip(10);相当于:select * from userInfo where id not in (select top 10 * from userInfo);19、查询在5-10之间的数据().limit(10).skip(5);注:可⽤于分页,limit是pageSize,skip是第⼏页*pageSize20、or 查询({$or: [{age: 22}, {age: 25}]});相当于:select * from userInfo where age = 22 or age = 25;21、查询第⼀条数据e();相当于:selecttop 1 * from userInfo;().limit(1);22、查询某个结果集的记录条数({age: {$gte: 25}}).count();相当于:select count(*) from userInfo where age >= 20;23、按_id查询⼀条记录lection('comments').find({"_id":ObjectId("5b179684e4b01fcf1c11c7d4")})相当于:select * from userInfo where id = ....;24、count查询记录条数().count()当使⽤limit()⽅法限制返回的记录数时,默认情况下count()⽅法仍然返回全部记录条数。 例如,下⾯的⽰例中返回的不是5,⽽是monitor表中所有的记录数量:().skip(10).limit(5).count();如果希望返回限制之后的记录数量,要使⽤count(true)或者count(⾮0):().skip(10).limit(5).count(true);25、查询某个字段存在的数据lection('monitor').find({warningThreshold:{$exists:true}})注:在mongodb中,如果实体类的某个字段(为string类型)为null,则在存⼊mongodb时该字段不会存⼊,如果是long类型,则存⼊为NumberLong(0);例:n(rName());为n(Unix());为0
三、索引:1、创建索引Index({name: 1});Index({name: 1, ts: -1});2、查询当前聚集集合所有索引exes();3、查看总索引记录⼤⼩ndexSize();4、读取当前集合的所有index信息x();5、删除指定索引dex("name_1");6、删除所有索引索引dexes();
四、修改、添加、删除集合数据1、添加({name: 'zhangsan', age: 25, sex: true});2、修改({age: 25}, {$set: {name: 'changeName'}}, false, true);相当于:update users set name = 'changeName' where age = 25;({name: 'Lisi'}, {$inc: {age: 50}}, false, true);相当于:update users set age = age + 50 where name = 'Lisi';({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);相当于:update users set age = age + 50, name = 'hoho' where name = 'Lisi';3、删除⼀条数据({age: 132});4、清空表数据();
五、其他1、循环添加数据> for (var i = 0; i < 30; i++) {... ({name: "u_" + i, age: 22 + i, sex: i % 2});... };这样就循环添加了30条数据,同样也可以省略括号的写法> for (var i = 0; i < 30; i++) ({name: "u_" + i, age: 22 + i, sex: i % 2});2、查询之前的错误信息vError();3、清除错误记录rror();
六、所遇到的坑今天遇到了这么个问题,就是根据相关字段查询特别慢,有时候直接就报ption了lection('spreadAggregationXj').find({"aggType" : "day" , "subjectId" : "5c7c8131f241255e462fc3a9" , "isAll" : "0" , "mediaId" : "1"})解决:在表上加相应字段的索引困惑:有的表没这些索引也不慢啊,为什么这个表就不⾏,是不是还有哪块的查询逻辑我没有发现呐
发布者:admin,转转请注明出处:http://www.yc00.com/news/1690367556a339075.html
评论列表(0条)