MongoDB常用命令大全

------mongodb启动 start----------
mongod -dbpath "D:\Program File\mongodb\data\db"
mongo
mongod --config "D:\Program File\mongodb\mongo.config" --install --serviceName "MongoDB" --journal  
------mongodb启动 end----------

命令来源-IT培训,软件开发


notes:mongo基于javascript的语法,MongoDb客户端:Robomongo 一、mongodb连接(port:27017) mongo 二、数据库操作 help use newdb show dbs 数据库为空的不会显示 db.dropDatabase(); 删除数据库 db.getName(); 当前使用的是哪个数据库 db 同上 db.stats(); 状态 db.version(); 版本号 db.getMongo() 机器地址 db.cloneDatabase(“127.0.0.1”); 从指定主机上克隆数据库 db.copyDatabase("mydb", "temp", "127.0.0.1"); 克隆指定数据库 db.repairDatabase(); 修复当前数据库 三、集合基本操作 db.createCollection("test",{size:20, capped:true, max:100}) db.getCollection("test"); 指定集合 db.getCollectionNames(); 所有集合 show collections 同上 db.printCollectionStats(); 所有集合状态 db.collection.drop() 删除集合 四、文档查 db.userInfo.find(); db.userInfo.distinct("name"); //distinct name db.userInfo.find({"age": 22}); //age = 22 db.userInfo.find({age: {$gt: 22}}); //age > 22 db.userInfo.find({age: {$lt: 22}}); //age < 22 db.userInfo.find({age: {$gte: 25}}); //age >= 25 db.userInfo.find({age: {$lte: 25}}); //age <= 25 db.userInfo.find({age: {$gte: 23, $lte: 26}}); //age >= 23 并且 age <= 26 db.userInfo.find({name: /mongo/}); //name like '%mongo%' db.userInfo.find({name: /^mongo/}); //name like 'mongo%' db.userInfo.find({}, {name: 1, age: 1}); //select name, age 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}); // name = 'zhangsan' and age = '22'; db.userInfo.find().limit(5); //limit 0,5 db.userInfo.find().skip(10); //大于10条 db.userInfo.find().limit(10).skip(5); //limit 5,10 db.userInfo.find({$or: [{age: 22}, {age: 25}]}); //age = 22 or age = 25 db.userInfo.findOne(); //limit 0,1 db.userInfo.find({age: {$gte: 25}}).count(); //select count(*) from userInfo where age >= 25 db.userInfo.find({sex: {$exists: true}}).count(); //select count(sex) from userInfo; 五、文档增删改 ----增加--- db.users.save({name: 'zhangsan', age: 25, sex: true}); //保存 ---修改---- db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true); //set: name = 'changeName' db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true); //inc: age = age + 50 db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true); //set,inc ---删除---- db.users.remove({age: 132}); ---查询修改删除 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 }); 六、用户操作 db.createUser( { user: "accountUser", pwd: "password", roles: [ "readWrite", "dbAdmin" ] } ) show users; //显示用户 db.auth("userName","123321"); //数据库认证,安全模式 db.removeUser("userName"); //删除用户 db.getPrevError(); //查询之前的错误信息 db.resetError(); //清除错误记录 七、索引 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(); //删除所有索引 八、Javascript常用编程 print("Hello World!"); 简单Hello World tojson(new Object('a')); 将一个对象转换成json for (var i = 0; i < 30; i++){ 循环添加数据 db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2}); } var cursor = db.users.find(); find 游标查询 db.users.find().forEach(printjson); forEach迭代循环 var cursor = db.users.find(); 将find游标当数组处理 var arr = db.users.find().toArray(); 将find游标转换成数组 db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson); 定制我们自己的查询结果 db.things.find({x:4}).forEach(function(x) {print(tojson(x));}); forEach传递函数显示信息

 

猜你喜欢

转载自www.cnblogs.com/cxxjohnson/p/9117027.html