-
./mongo localhost:27017 登录mongodb
-
use gmap; 切换数据库
-
db.auth('gmap','$Fortune2015'); 登录数据库
-
db.getName(); 获取当前数据库名称
-
db.stats(); 显示当前数据库状态
-
db.version(); 当前数据库版本
-
db.getMongo(); 显示当前db连接的服务器地址
-
db.getCollectionNames(); 获取所有聚集集合(类似于表)
-
db.getCollection("logs"); 获取某个集合
-
db.printCollectionStats(); 显示当前db所有聚集索引的状态
-
db.logs.find(); 默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”
-
但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize= 50;这样每页就显示50条记录了。 相当于:select* from logs;
-
db.logs.distinct("system"); 查询去掉后的当前聚集集合中的某列的重复数据 相当于:select distict system from logs;
-
db.logs.find({"userId":921}); 相当于 select * from
logs where userId=921;
-
db.logs.find({userId:{$gt:921}}); 相当于 select * from
logs where userId>921;
-
db.logs.find({userId:{$gte:921}}); 相当于 select * from
logs where userId>=921;
-
db.logs.find({userId:{$lt:921}}); 相当于 select * from
logs where userId<921;
-
db.logs.find({userId:{$lte:921}}); 相当于 select * from
logs where userId<=921;
-
db.logs.find({userId:{$gt:921,$lt:1000}}); 相当于 select * from
logs where userId>921 and userId < 1000
-
db.userInfo.find({name: /mongo/}); 相当于 select * from userInfo where name like ‘%mongo%';
-
db.userInfo.find({name: /^mongo/}); 相当于: select * from userInfo where name like
‘mongo%';
-
db.userInfo.find({},
{name: 1, age: 1}); 相当于:select
name, age from userInfo;当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。
-
db.userInfo.find({age: {$gt: 25}},
{name: 1, age: 1});相当于:select
name, age from userInfo where age >25;
-
升序:db.userInfo.find().sort({age:
1});
-
降序:db.userInfo.find().sort({age:
-1});
-
db.userInfo.find({name: 'zhangsan', age:
22});相当于:select
* from userInfo where name = ‘zhangsan' and age = ‘22';
-
db.userInfo.find().limit(5);相当于:select
* from userInfo limit 5;
-
查询在5-10之间的数据 db.userInfo.find().limit(10).skip(5);
-
db.userInfo.find({$or: [{age: 22}, {age:
25}]});相当于:select
* from userInfo where age = 22 or age = 25;
-
db.userInfo.findOne(); 相当于:select
* from userInfo limit 1;
-
db.userInfo.find({age: {$gte:
25}}).count();相当于:select
count(*) from userInfo where age >= 20;
-
db.userInfo.find({sex: {$exists:
true}}).count();相当于:select
count(sex) from userInfo;
-
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28624388/viewspace-2079621/
,如需转载,请注明出处,否则将追究法律责任。