mongodb常用命令

前阵子在使用mongodb,这里记录下常用的命令,待续未完...

DB:

从指定主机上克隆数据库: db.cloneDatabase(“127.0.0.1”); 将指定机器上的数据库的数据克隆到当前数据库
从指定的机器上复制指定数据库数据到某个数据库: db.copyDatabase(“mydb”, “temp”, “127.0.0.1”);将本机的mydb的数据复制到temp数据库中
修复当前数据库: db.repairDatabase();
查看当前使用的数据库:db.getName();  db; db和getName方法是一样的效果,都可以查询当前使用的数据库;
显示当前db状态: db.stats();
当前db版本: db.version();
查看当前db的链接机器地址:db.getMongo();

Collection:

创建一个聚集集合(table):db.createCollection(“collName”, {size: 20, capped: 5, max: 100});
得到指定名称的聚集集合(table):db.getCollection(“account”);
以集合的形式获得该数据库中集合名字:db.getCollectionNames();
显示当前db所有集合索引的状态:db.printCollectionStats(); 出现一堆数据。

用户相关:

1.mongodb不提供管理员账户,所有用户只能后续创建。所以,在搭建集群时,不开启安全认证机制,注释掉conf文件中部分内容:
	security:
            authorization: enabled
	    clusterAuthMode: keyFile
	    keyFile: /home/cuihuanyu01/mongodb/keyfile
	创建好用户在启动安全认证。
2.mongodb集群的安全验证是基于keyFile文件的,所以需要将同样的keyFile文件拷贝到所有机子上。
    keyfile文件类型可以是openssl rand -base64 745 > /home/cuihuanyu01/mongodb-keyfile 这样生成,也可以是个.txt文件。
3.用户权限也依赖与创建用户时所在的数据库,在admin下创建,则为管理员账户;在project下创建,则为普通用户;
    添加管理员用户:
	use admin
	db.createUser({user:”root”,pwd:”root”,roles:[{role:”root”,db:”admin”}]})
    创建普通用户:
	use project
	db.createUser({user:”user”,pwd:”user”,roles:[{role:”readWrite”,db:”project”}]})
    数据库认证、安全模式:db.auth(“userName”, “123123”);
    显示当前所有用户: show users;
    删除用户: db.removeUser(“userName”);
    修改用户密码: db.changeUserPassword('user','test'); 

其他:

    查询之前的错误信息: db.getPrevError();
    清除错误记录: db.resetError();

集合基本信息:

    查看帮助:  db.yourColl.help();
    查询当前集合的数据条数:  db.yourColl.count(); = db.yourColl.find().count();
    查看数据空间大小: db.userInfo.dataSize();
    得到当前聚集集合所在的db: db.userInfo.getDB();
    得到当前集合的状态 db.userInfo.stats();  --出现很多信息
    得到集合总大小 db.Ship.totalSize();
    聚集集合储存空间大小 db.ship.storageSize();
    Shard版本信息  db.userInfo.getShardVersion()
    聚集集合重命名 db.userInfo.renameCollection(“users”); 将userInfo重命名为users
    删除当前聚集集合 db.userInfo.drop();
    集群重命名:db.books.renameCollection(“newname”)	

集合操作:

    查询所有记录:db.userInfo.find();   相当于:select* from userInfo;
    查询去掉后的当前聚集集合中的某列的重复数据: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}});  相当于:select * from userInfo where age >22;
    查询age < 22的记录:db.userInfo.find({age: {$lt: 22}});   相当于:select * from userInfo where age <22;
    查询age >= 25的记录:db.userInfo.find({age: {$gte: 25}});   相当于:select * from userInfo where age >= 25;
    查询age <= 25的记录:db.userInfo.find({age: {$lte: 25}});
    查询age >= 23 并且 age <= 26:db.userInfo.find({age: {gte:23,lte: 26}});
    查询name中包含 mongo的数据:db.userInfo.find({name: /mongo/});   //相当于%% :select * from userInfo where name like ‘%mongo%’;
    查询name中以mongo开头的: db.userInfo.find({name: /^mongo/});  相当于:select * from userInfo where name like ‘mongo%’;
    查询指定列name、age数据: db.userInfo.find({}, {name: 1, age: 1});
        相当于:select name, age from userInfo;当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。
    查询指定列name、age数据, age > 25: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});
    查询name = zhangsan, age = 22的数据 : db.userInfo.find({name: ‘zhangsan’, age: 22});    相当于:select * from userInfo where name = ‘zhangsan’ and age = ‘22’;
    查询前5条数据: db.userInfo.find().limit(5);     相当于:selecttop 5 * from userInfo;
    查询10条以后的数据:db.userInfo.find().skip(10);    相当于:select * from userInfo where id not in (selecttop 10 * from userInfo);
    查询在5-10之间的数据:db.userInfo.find().limit(10).skip(5);  可用于分页,limit是pageSize,skip是第几页*pageSize;
    or与 查询: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);  相当于:selecttop 1 * from userInfo;
    查询某个结果集的记录条数:db.userInfo.find({age: {$gte: 25}}).count();   相当于:select count(*) from userInfo where age >= 20;
    如果要返回限制之后的记录数量,要使用count(true)或者count(非0) :db.users.find().skip(10).limit(5).count(true);
    按照某列进行排序:db.userInfo.find({sex: {$exists: true}}).count();   相当于:select count(sex) from userInfo;
索引:
    创建索引:db.userInfo.ensureIndex({name: 1});db.userInfo.ensureIndex({name: 1, ts: -1});
    查询当前聚集集合所有索引:db.userInfo.getIndexes();
    查看总索引记录大小:db.userInfo.totalIndexSize();
    读取当前集合的所有index信息:db.users.reIndex();
    删除指定索引:db.users.dropIndex(“name_1”); //删除这一项上的索引
    删除所有索引索引:db.users.dropIndexes();
修改、添加、删除集合数据:
1.添加:db.users.save({name: ‘zhangsan’, age: 25, sex: true}); 添加的数据的数据列,没有固定,根据添加的数据为准。
2.修改:db.collection.update(criteria, objNew, upsert, multi )
	criteria:update的查询条件,类似sql update查询内where后面的
	objNew:update的对象和一些更新的操作符(如,inc…)等,也可以理解为sql update查询内set后面的。
	upsert : 如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
	multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为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’;
3.删除:db.users.remove({age: 132});
4.查询修改删除:db.users.findAndModify({query: {age: {$gte: 25}}, sort: {age: -1},update: {set:name:‘a2′,inc: {age: 2}}, remove: true});
db.runCommand({ findandmodify : “users”,  query: {age: {$gte: 25}},sort: {age: -1},update: {set:name:‘a2′,inc: {age: 2}}, remove: true});

稀疏索引:sparse

创建稀疏索引:db.good.ensureIndex({"goodId": 1}, {"unique": true,"sparse":true});
唯一索引只允许一条索引字段为空的记录存在,之后就不允许插入了。再次插入 goodId 为 null 的记录时会报错:
	E11000 duplicate key error index: dup key: { : null };
“sparse”的作用就是当 goodId 在文档中不存在,或为空值,则不进入索引,从而避免上述问题。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值