您的当前位置:首页>全部文章>文章详情

mongodb常用命令

发表于:2019-10-15 11:57:04浏览:50次TAG:
### 官方文档 [https://docs.mongodb.com/manual/](https://docs.mongodb.com/manual/ "https://docs.mongodb.com/manual/") ### 链接数据库 ``` # 默认本地连接 mongo # 本地指定端口连接 mongo --port 28015 # 要明确指定主机名和/端口连接 mongo "mongodb://mongodb0.example.com:28015" mongo --host mongodb0.example.com:28015 mongo --host mongodb0.example.com --port 28015 # 指定身份的连接 mongo --username alice --password --authenticationDatabase admin --host mongodb0.examples.com --port 28015 ``` 为了更方便连接mongo的命令,我们以 `school` 为库名, 以 `users` 为表名 ### 使用help查看命令提示 ```bash db.help(); db.school.help(); db.school.find().help(); rs.help(); ``` ### 查询所有数据库 ```bash show dbs; ``` ### 切换/创建数据库 ```bash use school; # 当创建一个集合(表)的时候回自动创建当前数据库 ``` ### 删除当前使用数据库 ```bash db.dropDatabase(); ``` ### 从指定主机上克隆数据库 ``` db.cloneDatabase("127.0.0.1"); db.copyDatabase("school", "temp", "127.0.0.1"); ``` ### 修复当前数据库 ``` db.repairDatabase(); ``` ### 查看当前使用的数据库 ``` db.getName(); ``` ### 当前db版本 ``` db.version(); ``` ### 查看当前db的链接机器地址 ``` db.getMongo(); ``` ------------ ### 用户相关 #### 添加一个用户 ``` db.addUsers("name"); db.addUsers("usersName", "pwd123", true); # 用户名/密码/只读 ``` #### 数据库认证、安全模式 ``` db.auth("usersName", "123123"); ``` #### 显示当前所有用户 ``` show users; ``` #### 删除用户 ``` db.removeUsers("usersName"); ``` ------------ ### Collection聚集集合 #### 创建一个聚集集合(table) ``` db.createCollection("users", {size: 20, capped: 5, max: 100}); ``` #### 得到指定名称的聚集集合(table) ``` db.getCollection("class"); ``` #### 得到当前db的所有聚集集合 ``` db.getCollectionNames(); ``` #### 显示当前db所有聚集索引的状态 ``` db.printCollectionStats(); ``` ------------ ### 聚集集合查询 #### 查询所有记录 ``` db.users.find(); ``` 相当于 `select* from users;` 默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;” #### 查询去掉后的当前聚集集合中的某列的重复数据 ``` db.users.distinct("name"); # 会过滤掉name中的相同数据 ``` 相当于 `select distict name from users;` #### 查询age = 22的记录 [等于: : ;不等于: `$ne` ] ``` db.users.find({"age": 22}); ``` 相当于 `select * from users where age = 22;` #### 查询age > 22的记录 [ 大于: `$gt` ] ``` db.users.find({age: {$gt: 22}}); ``` 相当于 `select * from users where age >22;` #### 查询age < 22的记录 [ 小于: `$lt` ] ``` db.users.find({age: {$lt: 22}}); ``` 相当于 `select * from users where age <22;` #### 查询age >= 25的记录[ 大于等于: `$gte` ] ``` db.users.find({age: {$gte: 25}}); ``` 相当于 `select * from users where age >= 25;` #### 查询age <= 25的记录 [ 小于等于: `$lte` ] ``` db.users.find({age: {$lte: 25}}); ``` #### 查询age >= 23 并且 age <= 26 ``` db.users.find({age: {$gte: 23, $lte: 26}}); ``` #### 查询name中包含 mongo的数据 [ like : // ] ``` db.users.find({name: /mongo/}); # 相当于%% ``` 相当于 `select * from users where name like '%mongo%';` #### 查询name中包含 mongo的数据 正则匹配 [ like : regex ] ``` db.users.find({name: {regex: 'mo.*go' }); ``` 相当于 `select * from users where name like 'mo%ngo';` ``` db.users.find({name: {regex: /mongo$/ }); ``` 相当于 `select * from users where name like '%mongo'` ``` db.users.find({name: {regex: /^mongo/i }); ``` 相当于 `select * from users where name like 'mongo%'` 用法 ``` { : { $regex: /pattern/, $options: '' } } { : { $regex: 'pattern', $options: '' } } { : { $regex: /pattern/ } } ``` options 选项 `i` 大小写不敏感 `m` 查询匹配中使用了锚,例如^(代表开头)和$(代表结尾),以及匹配\n后的字符串 `x` 忽视所有空白字符 要求$regex与$option合用 `s` 允许点字符(.)匹配所有的字符,包括换行符。 要求$regex与$option合用 #### 查询name中以mongo开头的 ``` db.users.find({name: /^mongo/}); ``` 相当于 `select * from users where name like 'mongo%';` #### 查询指定列name/age数据 ``` db.users.find({}, {name: 1, age: 1}); ``` 相当于 `select name, age from users;` 当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。 #### 查询指定列name/age数据, age > 25 ``` db.users.find({age: {$gt: 25}}, {name: 1, age: 1}); ``` 相当于 `select name, age from users where age >25;` #### 按照年龄排序 [ 排序: sort (1 升序,-1 降序) ] 升序: ``` db.users.find().sort({age: 1}); ``` 降序: ``` db.users.find().sort({age: -1}); ``` #### 查询name = zhangsan, age = 22的数据 ``` db.users.find({name: 'zhangsan', age: 22}); ``` 相当于 `select * from users where name = 'zhangsan' and age = '22';` #### 查询前5条数据 ``` db.users.find().limit(5); ``` #### 查询10条以后的数据 ``` db.users.find().skip(10); ``` #### 查询在5条(含)的后10条之间的数据 ``` db.users.find().skip(5).limit(10); ``` #### or与 查询 [ 或者条件: `$or` ] ``` db.users.find({$or: [{age: 22}, {age: 25}]}); ``` 相当于 `select * from users where age = 22 or age = 25;` #### in与 查询 [ 包含条件: `$in` ] ``` db.users.find({age: {$in: [22, 23]}}); ``` 相当于 `select * from users where age in (22, 23);` #### not in与 查询 [ 不包含条件: `$nin` ] ``` db.users.find({age: {$nin: [22, 23]}}); ``` 相当于 `select * from users where age not in (22, 23);` #### 查询第一条数据 ``` db.users.findOne(); db.users.find().limit(1); ``` 相当于 `select top 1 * from users;` #### 查询某个结果集的记录条数 [ 查询总数: count() ] ``` db.users.find({age: {$gte: 25}}).count(); ``` 相当于 `select count(*) from users where age >= 20;` #### 按照某列进行排序 [ 字段是否存在: `$exists` ] ``` db.users.find({sex: {$exists: true}}).count(); ``` 相当于 `select count(sex) from users;` ------------ ### 修改、添加、删除集合数据 #### 添加 ``` db.users.save({name: ‘zhangsan’, age: 25, sex: true}); ``` 添加的数据的数据列,没有固定,根据添加的数据为准 #### 修改 update() 方法用于更新已存在的文档。语法格式如下: ``` db.collection.update( , , { upsert: , multi: , writeConcern: } ) ``` 参数说明: - `query` : update的查询条件,类似sql update查询内where后面的。 - `update` : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的 - `upsert` : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。 - `multi` : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。 - `writeConcern` :可选,抛出异常的级别。 ``` 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 }); ``` ``` db.runCommand({ findandmodify : "users", query: {age: {$gte: 25}}, sort: {age: -1}, update: {$set: {name: 'a2'}, $inc: {age: 2}}, remove: true }); ``` update 或 remove 其中一个是必须的参数; 其他参数可选。 ------------ ### 索引 #### 创建索引 ``` db.users.ensureIndex({name: 1}); db.users.ensureIndex({name: 1, ts: -1}); ``` #### 查询当前聚集集合所有索引 ``` db.users.getIndexes(); ``` #### 查看总索引记录大小 ``` db.users.totalIndexSize(); ``` #### 读取当前集合的所有index信息 ``` db.users.reIndex(); ``` #### 删除指定索引 ``` db.users.dropIndex("name_1"); ``` #### 删除所有索引索引 ``` db.users.dropIndexes(); ```
栏目分类全部>
腾讯云采购季云服务器一折促销