ITPub博客

首页 > 应用开发 > IT综合 > Mongodb 常用操作命令

Mongodb 常用操作命令

原创 IT综合 作者:百联达 时间:2016-04-12 11:50:55 0 删除 编辑


  1. ./mongo localhost:27017                  登录mongodb
  2. use gmap;          切换数据库
  3. db.auth('gmap','$Fortune2015');   登录数据库
  4. db.getName();   获取当前数据库名称
  5. db.stats();       显示当前数据库状态
  6. db.version();       当前数据库版本
  7. db.getMongo();    显示当前db连接的服务器地址
  8. db.getCollectionNames();  获取所有聚集集合(类似于表) 
  9. db.getCollection("logs");  获取某个集合 
  10. db.printCollectionStats();  显示当前db所有聚集索引的状态 
  11. db.logs.find(); 默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”
  12. 但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize= 50;这样每页就显示50条记录了。 相当于:select* from logs;
  13. db.logs.distinct("system");  查询去掉后的当前聚集集合中的某列的重复数据  相当于:select distict system from logs;
  14. db.logs.find({"userId":921});       相当于 select * from logs where userId=921; 
  15. db.logs.find({userId:{$gt:921}});   相当于 select * from logs where userId>921;
  16. db.logs.find({userId:{$gte:921}});   相当于 select * from logs where userId>=921;
  17. db.logs.find({userId:{$lt:921}});   相当于 select * from logs where userId<921;
  18. db.logs.find({userId:{$lte:921}});   相当于 select * from logs where userId<=921; 
  19. db.logs.find({userId:{$gt:921,$lt:1000}});   相当于 select * from logs where userId>921 and userId < 1000
  20. db.userInfo.find({name: /mongo/});  相当于 select * from userInfo where name like ‘%mongo%';
  21. db.userInfo.find({name: /^mongo/}); 相当于:  select * from userInfo where name like ‘mongo%';
  22. db.userInfo.find({}, {name: 1, age: 1});  相当于:select name, age from userInfo;当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。
  23. db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});相当于:select name, age from userInfo where age >25;
  24. 升序:db.userInfo.find().sort({age: 1});
  25. 降序:db.userInfo.find().sort({age: -1});
  26. db.userInfo.find({name: 'zhangsan', age: 22});相当于:select * from userInfo where name = ‘zhangsan' and age = ‘22';
  27. db.userInfo.find().limit(5);相当于:select * from userInfo limit 5; 
  28. 查询在5-10之间的数据 db.userInfo.find().limit(10).skip(5);
  29. db.userInfo.find({$or: [{age: 22}, {age: 25}]});相当于:select * from userInfo where age = 22 or age = 25;
  30. db.userInfo.findOne();  相当于:select  * from userInfo limit 1;
  31. db.userInfo.find({age: {$gte: 25}}).count();相当于:select count(*) from userInfo where age >= 20;
  32. db.userInfo.find({sex: {$exists: true}}).count();相当于:select count(sex) from userInfo;


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28624388/viewspace-2079621/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
15年以上互联网经验,先后从事过制造业,证券业,物业行业和物流行业信息系统和互联网产品的设计落地实施,8年系统架构经验。最近关注企业数字化转型、财务数字化服务共享、大语言模型、Kubernetes微服务架构和Istio微服务治理框架。

注册时间:2013-02-05

  • 博文量
    332
  • 访问量
    1265622