> For the complete documentation index, see [llms.txt](https://rowkey-books.gitbook.io/pragmatic-java-engineer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rowkey-books.gitbook.io/pragmatic-java-engineer/mongo-usage.md).

# 附录D: MongoDB常用命令

MongoDB版本：3.2.7

## 1. 基本操作

* db.getMongo()：取得当前服务器的连接对象。
* db.createUser(user, writeConcern) ：添加用户。
* db.dropUser(username)：删除用户。
* db.system.users.find()：查看系统所有用户列表。
* db.system.users.remove({user:"mongouser"})： 删除用户。
* db.getUsers()：查看当前数据库的用户。
* db.auth(usrename,password)；验证用户。
* db.getName()：返回当操作数据库的名称。
* db.createCollection(name)：创建一个数据集。
* db.currentOp()：查看数据库的当前操作。
* db.dropDataBase()：删除当前数据库。
* db.getCollection(collectonName)：取得一个数据集合。
* db.getCollenctionNames()：取得所有数据集合的名称列表。
* db.getLastError()：返回最后一个错误的提示消息。
* db.getLastErrorObj()：返回最后一个错误的对象。
* db.getReplicationInfo()：获得复制集的信息。
* db.printReplicationInfo()：打印复制集的信息。
* db.printCollectionStats()：返回当前库的数据集合状态。
* db.printSlaveReplicationInfo()：打印从数据库的复制集信息。
* db.printShardingStatus()：打印分片状态。
* db.commandHelp(command)：显示命令的帮助信息。
* db.runCommand(cmdObj)：运行一个数据库命令。
* db.setProfilingLevel(level，slowms)：设置数据库的优化级别(0=off,1=slow,2=all)以及慢查询的耗时阈值。
* db.getProfilingStatus()：获取数据库的优化级别和慢查询的耗时阈值。
* db.system.profile.find()：查看收集到的慢查询。
* db.version()：返回当前程序的版本信息。
* db.serverStatus().connections：连接数信息,其中current数值+available数值就是当前mongodb最大连接数。
* db.serverStatus().mem：内存占用信息。
* db.cloneDataBase(fromhost)：从目标服务器克隆一个数据库。
* db.copyDatabase(fromdb,todb,fromhost)：复制数据库:fromdb-源数据库名称，todb-目标数据库名称，fromhost-源数据库服务器地址。
* db.repairDatabase()：修复当前数据库。
* db.killOp()：停止/杀死在当前库的当前操作。
* db.shutdownServer()：安全关闭当前服务程序。

## 2. 数据集操作

* db.test\_collection.drop()：删除数据集。
* db.createCollection(“test\_collection”)：创建集合。
* db.test\_collection.renameCollection("test\_collection1")：重命名集合。
* db.test\_collection.find({status:1})：返回test\_collection数据集status=1的数据集。
* db.test\_collection.find({status:1}}).count()：返回test\_collection数据集中status=1的数据总数。
* db.test\_collection.find({status:1}).limit(3)：返回test\_collection数据集中status=1的前三条数据。
* db.test\_collection.find({status:1}).skip(2)：返回test\_collection数据集中status=1的从第三条开始的数据。
* db.test\_collection.find({status:1}).limit(24).skip(8)：返回test\_collection数据集中status=1的从第九条开始的24条数据。
* db.test\_collection.find({status:1}}).sort()：返回test\_collection数据集中status=1的有序数据。
* db.test\_collection.findOne(\[query]) 返回符合条件的一条数据。
* db.test\_collection.getIndexes()：返回此数据集的索引信息。
* db.test\_collection.mapReduce(mayFunction,reduceFunction)：执行MapReduce操作。
* db.test\_collection.remove(query)：在数据集中删除一条数据。
* db.test\_collection.remove({})：清空数据集合。
* db.test\_collection.save(obj)：往数据集中插入/更新一条数据。
* db.test\_collection.stats()：返回此数据集的状态。
* db.test\_collection.storageSize()：返回此数据集的存储大小
* db.test\_collection.totalIndexSize()：返回此数据集的索引文件大小。
* db.test\_collection.totalSize()：返回此数据集的总大小。
* db.test\_collection.update(query,object\[,upsert\_bool])：在此数据集中更新一条数据。
* db.test\_collection.createIndex(keys\[,options])：创建索引。
* db.test\_collection.getIndexes()：查看索引。
* db.test\_collection.dropIndex('\[indexName]')：删除索引。

## 3. MongoDB语法与关系型数据库SQL语法比较

* db.test\_collection.find({'name':'testname'}) <-> select \* from test\_collection where name='testname'
* db.test\_collection.find({$or:\[{'name':'testname'},{'name':'testname2'}]}) <-> select \* from test\_collection where name='testname' or testname='testname2'
* db.test\_collection.find() <-> select \* from test\_collection
* db.test\_collection.find({'status':1}).count() <-> select count(\*) from test\_collection where status=1
* db.test\_collection.find().skip(10).limit(20) <-> select \* from test\_collection limit 10,20
* db.test\_collection.find({'status':{$in:\[1,2]}}) <-> select \* from test\_collection where status in (1,2)
* db.test\_collection.find().sort({'status':-1}) <-> select \* from test\_collection order by status desc
* db.test\_collection.distinct('name',{'age':{$lt:25}}) <-> select distinct(name) from test\_collection where age < 1
* db.test\_collection.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.star;},initial:{msum:0}}) <-> select name,sum(stat) from test\_collection group by name
* db.test\_collection.find('this.age<25',{name:1}) <-> select name from test\_collection where age < 20
* db.test\_collection.insert({'name':'testname','age':25})<->insert into test\_collection ('name','age') values('testname',25)
* db.test\_collection.remove({}) <-> delete from test\_collection
* db.test\_collection.remove({'age':25}) <-> delete from test\_collection where age=25
* db.test\_collection.remove({'age':{$lt:20}}) <-> delete from test\_collection where age<25
* db.test\_collection.remove({'age':{$lte:20}}) <-> delete from test\_collection where age<=25
* db.test\_collection.remove({'age':{$gt:20}}) <-> delete from test\_collection where age>25
* db.test\_collection.remove({'age':{$gte:20}}) <-> delete from test\_collection where age>=2
* db.test\_collection.remove({'age':{$ne:20}}) <-> delete from test\_collection where age!=25
* db.test\_collection.updateMany({'name':'testname'},{$set:{'age':30}}) <-> update test\_collection set age=30 where name='testname'
* db.test\_collection.updateMany({'name':'testname'},{$inc:{'age':2}}) <-> update test\_collection set age=age+2 where name='testname'
* db.test\_collection.find({name: /testname/}) <-> select \* from test\_collection where name like ‘%testname%’;
* db.test\_collection.find({name: /^testname/}) <-> select \* from test\_collection where name like ‘testname%’;

## 4. 开启安全认证

### 创建管理员用户

```
use admin
db.createUser(
    {
        user: "root",
        pwd: "root123",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] #roles设置为root则为超级用户权限
    }
)

mongod --auth --port 27017 --dbpath /data/db --authenticationDatabase "admin"
use admin;
db.auth("root","root123");
```

### 创建用户

```
use test_db;
db.createUser(
    {
        user: "mongouser",
        pwd: "mongo123",
        roles: [
            { role: "readWrite", db: "test_db" }
        ]
    }
)
```

### 修改密码

```
db.changeUserPassword("mongouser", "123456”)
```

### 获取某用户的权限信息

```
db.getUser("mongouser")
```

### 获取某角色的权限信息

```
db.getRole( "read", { showPrivileges: true } )
```

### 赋予权限

```
use test_db;
db.grantRolesToUser(
    "mongouser",
    [
      { role: "read", db:  “test_db" }
    ]
)
```

### 删除权限

```
use test_db;
db.revokeRolesFromUser(
    "mongouser",
    [
      { role: "readWrite", db: "test_db" }
    ]
)
```
