├── README.md ├── 下载链接 ├── 指令 └── New Document.md /README.md: -------------------------------------------------------------------------------- 1 | # mongodb 2 | mongodb指令使用手册 3 | -------------------------------------------------------------------------------- /下载链接: -------------------------------------------------------------------------------- 1 | http://www.mongodb.org/downloads 2 | 链接:http://pan.baidu.com/s/1jHDL7xW 密码:vss0 3 | 4 | 可视化数据库工具 5 | Robomongo 6 | 链接:http://pan.baidu.com/s/1i5Ih557 密码:ho8l 7 | -------------------------------------------------------------------------------- /指令: -------------------------------------------------------------------------------- 1 | Mongodb术语 2 | 3 | 一、下载 4 | http://www.mongodb.org/downloads 5 | 6 | 在安装的bin文件夹下打开控制台 要先创建一个数据库文件夹 7 | C:\mongodb-win32-x86_64-3.0.6\bin>mongod.exe --dbpath= 8 | 比如在d盘。。。创建text文件夹 复制文件夹路径 9 | D:\Documents\Desktop\js(3) \nodejs\day4\test 10 | 11 | C:\mongodb-win32-x86_64-3.0.6\bin>mongod.exe --dbpath= D:\Documents\Desktop\js(3) \nodejs\day4\test回车 12 | 这个过程叫创建数据库目录 另在bin文件夹下打开控制台并在此输入指令 创建数据库目录的指令不要关闭才可以运行控制指令 13 | 再在bin文件夹下打开指令 输入 14 | mongo.exe回车 就可以进行其他指令命令 15 | use test107 创建test1707文件夹 切换数据库 16 | show dbs 查询数据库 17 | db/db.getName() 查看当前使用的数据库 18 | db.createCollection(“users”)创建users聚集集合 19 | db.getCollection(“users”)得到指定的聚集集合 20 | (3) 得到当前db的所有聚集集合 21 | db.getCollectionNames(); 22 | (4) 显示当前db所有聚集的状态 23 | db.printCollectionStats(); 24 | 25 | (1) 添加 26 | db.users.save({name: ‘zhangsan', age: 25, sex: true}); 27 | db.users.save([{name: ‘zhangsan', age: 25, sex: true},{name:”kerin”,age:100}]); 28 | (2) 修改 29 | db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true); 30 | 相当于:update users set name = ‘changeName' where age = 25;//将符合age 25的 是否插入新的name值 31 | False为默认 不插入 true为插入 32 | db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true); 33 | 相当于:update users set age = age + 50 where name = ‘Lisi';//将符合name Lisi的 34 | 是否只更改第一条age为50 false为默认 只更改第一条 true为符合条件的全部更新 35 | db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: kerwin'}}, false, true); 36 | 相当于:update users set age = age + 50, name = ‘hoho' where name = ‘kerwin'; 37 | (3) 删除 38 | db.users.remove({age: 132}); 39 | 40 | (1) 查询所有记录 41 | db.userInfo.find(); 42 | 相当于:select* from userInfo; 43 | (2) 查询某字段去重后数据 44 | db.userInfo.distinct("name"); 45 | 相当于:select distict name from userInfo; 46 | (3) 查询age = 22的记录 47 | db.userInfo.find({"age": 22}); 48 | 相当于: select * from userInfo where age = 22; 49 | (4) 查询age > 22的记录 50 | db.userInfo.find({age: {$gt: 22}}); 51 | 相当于:select * from userInfo where age >22; 52 | (5) 查询age < 22的记录 53 | db.userInfo.find({age: {$lt: 22}}); 54 | 相当于:select * from userInfo where age <22 55 | 56 | (6) 查询age >= 25的记录 57 | db.userInfo.find({age: {$gte: 25}}); 58 | 相当于:select * from userInfo where age >= 25; 59 | (7) 查询age <= 25的记录 60 | db.userInfo.find({age: {$lte: 25}}); 61 | (8) 查询age >= 23 并且 age <= 26 62 | db.userInfo.find({age: {$gte: 23, $lte: 26}}); 63 | (9) 查询name中包含 mongo的数据 64 | db.userInfo.find({name: /mongo/}); 65 | //相当于%% 66 | select * from userInfo where name like ‘%mongo%’; 67 | (10) 查询name中以mongo开头的 68 | db.userInfo.find({name: /^mongo/}); 69 | select * from userInfo where name like ‘mongo%’; 70 | 71 | (11) 查询指定列name、age数据(想显示哪列,就将字段设置为1,不想显示就设置成0) 72 | db.userInfo.find({}, {name: 1, age: 1}); 73 | 相当于:select name, age from userInfo; 74 | (12) 查询指定列name、age数据, age > 25 75 | db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1}); 76 | 相当于:select name, age from userInfo where age >25; 77 | (13) 按照年龄排序(写成数组就是多列查询) 78 | 升序:db.userInfo.find().sort({age: 1}); 79 | 降序:db.userInfo.find().sort({age: -1}); 80 | (14) 查询name = zhangsan, age = 22的数据 81 | db.userInfo.find({name: 'zhangsan', age: 22}); 82 | 相当于:select * from userInfo where name = ‘zhangsan' and age = ’22'; 83 | (15) 查询前5条数据 84 | db.userInfo.find().limit(5); 85 | 相当于:select top 5 * from userInfo; 86 | 87 | (16) 查询10条以后的数据 88 | db.userInfo.find().skip(10); 89 | 相当于:select * from userInfo where id not in ( 90 | select top 10 * from userInfo 91 | ); 92 | (17) 查询在5-10之间的数据 93 | db.userInfo.find().skip(5).limit(10); //打印6 7 8 9 10 条 94 | 实际中写法db.userInfo.find().skip(5*n).limit(10); 95 | (18) or与 查询 96 | db.userInfo.find({$or: [{age: 22}, {age: 25}]}); 97 | 相当于:select * from userInfo where age = 22 or age = 25; 98 | (19) 查询第一条数据 99 | db.userInfo.findOne(); 100 | 相当于:selecttop 1 * from userInfo; 101 | db.userInfo.find().limit(1); 102 | (20) 查询某个结果集的记录条数 103 | db.userInfo.find({age: {$gte: 25}}).count(); 104 | 相当于:select count(*) from userInfo where age >= 20; 105 | 106 | 107 | 其他 108 | (1) Help查看命令提示 109 | help 110 | db.help() 111 | db.test.help() 112 | db.test.find().help() 113 | (2) 创建/切换数据库 114 | use music 115 | (3) 查询数据库 116 | show dbs 117 | (4) 查看当前使用的数据库 118 | db/db.getName() 119 | (5) 显示当前DB状态 120 | db.stats() 121 | (6) 查看当前DB版本 122 | db.version() 123 | (7) 查看当前DB的链接机器地址 124 | db.getMongo() 125 | (8) 删除数据库 126 | db.dropDatabase() 127 | 128 | robomongo可视化使用creat 129 | name随便 130 | add localhost 27017是 131 | -------------------------------------------------------------------------------- /New Document.md: -------------------------------------------------------------------------------- 1 | Mongodb术语 2 | ![ss](http://pan.baidu.com/s/1bp4M7G7) 3 | 一、下载 4 | http://www.mongodb.org/downloads 5 | 6 | 在安装的bin文件夹下打开控制台 要先创建一个数据库文件夹 7 | C:\mongodb-win32-x86_64-3.0.6\bin>mongod.exe --dbpath= 8 | 比如在d盘。。。创建text文件夹 复制文件夹路径 9 | D:\Documents\Desktop\js(3) \nodejs\day4\test 10 | 11 | C:\mongodb-win32-x86_64-3.0.6\bin>mongod.exe --dbpath= D:\Documents\Desktop\js(3) \nodejs\day4\test回车 12 | 这个过程叫创建数据库目录 另在bin文件夹下打开控制台并在此输入指令 创建数据库目录的指令不要关闭才可以运行控制指令 13 | 再在bin文件夹下打开指令 输入 14 | mongo.exe回车 就可以进行其他指令命令 15 | use test107 创建test1707文件夹 切换数据库 16 | show dbs 查询数据库 17 | db/db.getName() 查看当前使用的数据库 18 | db.createCollection(“users”)创建users聚集集合 19 | db.getCollection(“users”)得到指定的聚集集合 20 | (3) 得到当前db的所有聚集集合 21 | db.getCollectionNames(); 22 | (4) 显示当前db所有聚集的状态 23 | db.printCollectionStats(); 24 | 25 | (1) 添加 26 | db.users.save({name: ‘zhangsan', age: 25, sex: true}); 27 | db.users.save([{name: ‘zhangsan', age: 25, sex: true},{name:”kerin”,age:100}]); 28 | (2) 修改 29 | db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true); 30 | 相当于:update users set name = ‘changeName' where age = 25;//将符合age 25的 是否插入新的name值 31 | False为默认 不插入 true为插入 32 | db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true); 33 | 相当于:update users set age = age + 50 where name = ‘Lisi';//将符合name Lisi的 34 | 是否只更改第一条age为50 false为默认 只更改第一条 true为符合条件的全部更新 35 | db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: kerwin'}}, false, true); 36 | 相当于:update users set age = age + 50, name = ‘hoho' where name = ‘kerwin'; 37 | (3) 删除 38 | db.users.remove({age: 132}); 39 | 40 | (1) 查询所有记录 41 | db.userInfo.find(); 42 | 相当于:select* from userInfo; 43 | (2) 查询某字段去重后数据 44 | db.userInfo.distinct("name"); 45 | 相当于:select distict name from userInfo; 46 | (3) 查询age = 22的记录 47 | db.userInfo.find({"age": 22}); 48 | 相当于: select * from userInfo where age = 22; 49 | (4) 查询age > 22的记录 50 | db.userInfo.find({age: {$gt: 22}}); 51 | 相当于:select * from userInfo where age >22; 52 | (5) 查询age < 22的记录 53 | db.userInfo.find({age: {$lt: 22}}); 54 | 相当于:select * from userInfo where age <22 55 | 56 | (6) 查询age >= 25的记录 57 | db.userInfo.find({age: {$gte: 25}}); 58 | 相当于:select * from userInfo where age >= 25; 59 | (7) 查询age <= 25的记录 60 | db.userInfo.find({age: {$lte: 25}}); 61 | (8) 查询age >= 23 并且 age <= 26 62 | db.userInfo.find({age: {$gte: 23, $lte: 26}}); 63 | (9) 查询name中包含 mongo的数据 64 | db.userInfo.find({name: /mongo/}); 65 | //相当于%% 66 | select * from userInfo where name like ‘%mongo%’; 67 | (10) 查询name中以mongo开头的 68 | db.userInfo.find({name: /^mongo/}); 69 | select * from userInfo where name like ‘mongo%’; 70 | 71 | (11) 查询指定列name、age数据(想显示哪列,就将字段设置为1,不想显示就设置成0) 72 | db.userInfo.find({}, {name: 1, age: 1}); 73 | 相当于:select name, age from userInfo; 74 | (12) 查询指定列name、age数据, age > 25 75 | db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1}); 76 | 相当于:select name, age from userInfo where age >25; 77 | (13) 按照年龄排序(写成数组就是多列查询) 78 | 升序:db.userInfo.find().sort({age: 1}); 79 | 降序:db.userInfo.find().sort({age: -1}); 80 | (14) 查询name = zhangsan, age = 22的数据 81 | db.userInfo.find({name: 'zhangsan', age: 22}); 82 | 相当于:select * from userInfo where name = ‘zhangsan' and age = ’22'; 83 | (15) 查询前5条数据 84 | db.userInfo.find().limit(5); 85 | 相当于:select top 5 * from userInfo; 86 | 87 | (16) 查询10条以后的数据 88 | db.userInfo.find().skip(10); 89 | 相当于:select * from userInfo where id not in ( 90 | select top 10 * from userInfo 91 | ); 92 | (17) 查询在5-10之间的数据 93 | db.userInfo.find().skip(5).limit(10); //打印6 7 8 9 10 条 94 | 实际中写法db.userInfo.find().skip(5*n).limit(10); 95 | (18) or与 查询 96 | db.userInfo.find({$or: [{age: 22}, {age: 25}]}); 97 | 相当于:select * from userInfo where age = 22 or age = 25; 98 | (19) 查询第一条数据 99 | db.userInfo.findOne(); 100 | 相当于:selecttop 1 * from userInfo; 101 | db.userInfo.find().limit(1); 102 | (20) 查询某个结果集的记录条数 103 | db.userInfo.find({age: {$gte: 25}}).count(); 104 | 相当于:select count(*) from userInfo where age >= 20; 105 | 106 | 107 | 其他 108 | (1) Help查看命令提示 109 | help 110 | db.help() 111 | db.test.help() 112 | db.test.find().help() 113 | (2) 创建/切换数据库 114 | use music 115 | (3) 查询数据库 116 | show dbs 117 | (4) 查看当前使用的数据库 118 | db/db.getName() 119 | (5) 显示当前DB状态 120 | db.stats() 121 | (6) 查看当前DB版本 122 | db.version() 123 | (7) 查看当前DB的链接机器地址 124 | db.getMongo() 125 | (8) 删除数据库 126 | db.dropDatabase() 127 | 128 | robomongo可视化使用creat 129 | name随便 130 | add localhost 27017是 131 | --------------------------------------------------------------------------------