本篇文章介绍学习MongoDB的一些常用命令,希望能帮助大家.
Help查看命令提示db.help();db.yourColl.help();db.youColl.find().help();use yourDB; 当创建一个集合(table)的时候会自动创建当前数据库show dbs;db.dropDatabase();db.copyDatabase("mydb", "temp", "127.0.0.1");将本机的mydb的数据复制到temp数据库中db.repairDatabase();db.getName();db.stats();db.version();db.getMongo();table)db.createCollection(“collName”, {size: 20, capped: 5, max: 100});table)db.getCollection("account");db.getCollectionNames();db.printCollectionStats();db.addUser("name");db.addUser("userName", "pwd123", true); 添加用户、设置密码、是否只读db.auth("userName", "123123");show users;db.removeUser("userName");db.userInfo.find(); 相当于:select* from userInfo;默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize= 50;这样每页就显示50条记录了。db.userInfo.distinct("name");会过滤掉name中的相同数据,相当于:select distict name from userInfo;age = 22的记录db.userInfo.find({"age": 22}); 相当于:select * from userInfo where age = 22;age > 22的记录db.userInfo.find({age: {$gt: 22}});age < 22的记录db.userInfo.find({age: {$lt: 22}});age >= 25的记录db.userInfo.find({age: {$gte: 25}});age >= 23 并且 age <= 26db.userInfo.find({age: {$gte: 23, $lte: 26}});name中包含 mongo的数据db.userInfo.find({name: /mongo/});name中以mongo开头的db.userInfo.find({name: /^mongo/});name、age数据db.userInfo.find({}, {name: 1, age: 1});当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。name、age数据, age > 25.db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});db.userInfo.find().sort({age: 1});db.userInfo.find().sort({age: -1});name = zhangsan, age = 22的数据db.userInfo.find({name: 'zhangsan', age: 22});db.userInfo.find().limit(5);db.userInfo.find().skip(10);db.userInfo.find().limit(10).skip(5);可用于分页,limit是pageSize,skip是第几页*pageSizedb.userInfo.find({$or: [{age: 22}, {age: 25}]});db.userInfo.findOne();db.userInfo.find().limit(1);db.userInfo.find({age: {$gte: 25}}).count();如果要返回限制之后的记录数量,要使用count(true)或者count(非0)db.users.find().skip(10).limit(5).count(true);db.userInfo.find({sex: {$exists: true}}).count();db.userInfo.ensureIndex({name: 1});db.userInfo.ensureIndex({name: 1, ts: -1});db.userInfo.getIndexes();db.userInfo.totalIndexSize();db.users.reIndex();db.users.dropIndex("name_1");db.users.dropIndexes();