数据库 发布日期:2024/12/29 浏览次数:1
help db.help(); db.yourColl.help();
use raykaeso;
当创建一个集合(table)的时候会自动创建当前数据库
show dbs;
db.dropDatabase();
db.cloneDatabase(“127.0.0.1”);
将指定机器上的数据库的数据克隆到当前数据库
db.copyDatabase(“mydb”, “temp”, “127.0.0.1”);
将本机的mydb的数据复制到temp数据库中
db.repairDatabase();
db.getName()/db;
db.stats();
db.version();
db.getMongo();
db.getPrevError(); db.resetError();
db.createCollection(“collName”, {size: 20, capped: 5, max: 100});//创建成功会显示{“ok”:1} //判断集合是否为定容量db.collName.isCapped();
db.getCollection(“account”);
db.getCollectionNames();
db.printCollectionStats();
db.yourColl.count();
db.yourColl.dataSize();
db.yourColl.getDB();
db.coll.stats();
db.coll.totalSize();
db.coll.storageSize();
db.coll.renameCollection(“ray”);
将coll重命名为ray
db.coll.drop();
db.createUser({user: 'username', pwd: 'xxxx', roles: [{role: 'readWrite', db: 'dbname'}]});
添加用户、设置密码、是否只读
db.auth(“ray”, “123456”);
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;
db.userInfo.find({“age”: 22});
相当于: select * from userInfo where age = 22;
MongoDB中条件操作符有:
(>) 大于 – $gt
(<) 小于 – $lt (>=) 大于等于 – $gte
(<= ) 小于等于 – $lte
db.userInfo.find({age: {$gt: 22}}); 相当于:select * from userInfo where age>22; db.userInfo.find({age: {$lt: 22}}); 相当于:select * from userInfo where age<22; db.userInfo.find({age: {$gte: 25}}); 相当于:select * from userInfo where age >= 25;
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
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().limit(5); 相当于:select * from userInfo limit 5;
db.userInfo.find().skip(10); 相当于:select count() from userInfo as total; select from userInfo limit 10,total;
db.userInfo.find().limit(10).skip(5);
可用于分页,limit是pageSize,skip是第几页pageSize
相当于:select from userInfo limit 5,10;
db.userInfo.find({$or: [{age: 22}, {age: 25}]}); 相当于:select * from userInfo where age = 22 or age = 25;
db.userInfo.findOne(); db.userInfo.find().limit(1); 相当于:select * from userInfo limit 1;
db.userInfo.find({age: {$gte: 25}}).count(); 相当于:select count(*) from userInfo where age >= 20;
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();
db.users.save({name: ‘zhangsan', age: 25, sex: true});
添加的数据的数据列,没有固定,根据添加的数据为准
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';
db.users.remove({age: 132});
db.users.findAndModify({ query: {age: {$gte: 25}}, sort: {age: -1}, update: {$set: {name: ‘a2′}, $inc: {age: 2}}, remove: true });
更多关于MongoDB常用数据库命令文章请查看下面的相关链接