
# 更新用户
**db.updateUser(username, update, writeConcern）**
- 命令中参数username为要更新的用户名。
- update为文档类型，包含用户替换数据的文档。
- writeConcern为可选参数， 更新操作的write concern级别。
```
db.updateUser(
   "<username>",
   {
     customData : { <any information> },
     roles : [
       { role: "<role>", db: "<database>" } | "<role>",
       ...
     ],
     pwd: passwordPrompt(),      // Or  "<cleartext password>"
     authenticationRestrictions: [
        {
          clientSource: ["<IP>" | "<CIDR range>", ...],
          serverAddress: ["<IP>", | "<CIDR range>", ...]
        },
        ...
     ],
     mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ],
     passwordDigestor: "<server|client>"
   },
   writeConcern: { <write concern> }
)
```
表1update参数说明 
| 字段                         | 类型     | 说明                                                             |
|:---|:---|:---|
| customData                 | 文档     | 可选。任意信息                                                        |
| roles                      | 数组     | 可选。授予用户的角色。 roles数组的更新将覆盖以前的数组的值。                              |
| pwd                        | string | 可选。用户密码                                                        |
| authenticationRestrictions | 数组     | 可选。服务器对用户实施的身份验证限制，用于指定该角色可接入的IP地址或者IP地址段。                     |
| mechanisms                 | 数组     | 可选的。指定用于创建SCRAM用户凭据的特定SCRAM机制。当前只包含SCRAM-SHA-1 和SCRAM-SHA-256。 |
| passwordDigestor           | string | 可选的。指示是在server端还是client端验证密码，默认是server                         |
   
#### 示例
- **更新用户信息**
  products数据库中的用户appClient01，其信息如下：
  ```
  {
  "_id" : "products.appClient01",
  "token" : NumberLong("8424642624807814713"),
  "user" : "appClient01",
  "db" : "products",
  "customData" : {
  "empID" : "12345",
  "badge" : "9156"
  },
  "roles" : [
  {
  "role" : "readWrite",
  "db" : "products"
  },
  {
  "role" : "read",
  "db" : "inventory"
  }
  ],
  "mechanisms" : [
  "SCRAM-SHA-1",
  "SCRAM-SHA-256"
  ]
  }
  ```
  下面操作会替换用户的自定义数据和角色数据：
  ```
  use products
  db.updateUser( "appClient01",
  {
     customData : { employeeId : "0x3039" },
     roles : [
        { role : "read", db : "assets"  }
     ]
  } )
  ```
  products数据库中的用户appClient01，经过更新后信息如下：
  ```
  {
  "_id" : "products.appClient01",
  "token" : NumberLong("8424642624807814713"),
  "user" : "appClient01",
  "db" : "products",
  "customData" : {
  "employeeId" : "0x3039"
  },
  "roles" : [
  {
  "role" : "read",
  "db" : "assets"
  }
  ],
  "mechanisms" : [
  "SCRAM-SHA-1",
  "SCRAM-SHA-256"
  ]
  }
  ```
  
- **更新用户以仅使用SCRAM-SHA-256凭证**
  reporting数据库中的用户reportUser256，其信息如下：
  ```
  {
  "_id" : "reporting.reportUser256",
  "token" : NumberLong("2827251846225877395"),
  "user" : "reportUser256",
  "db" : "reporting",
  "roles" : [ ],
  "mechanisms" : [
  "SCRAM-SHA-1",
  "SCRAM-SHA-256"
  ]
  }
  ```
  以下操作会将当前同时拥有 SCRAM-SHA-256 和 SCRAM-SHA-1 全权证书的用户更新为只拥有 SCRAM-SHA-256 全权证书的用户。
  ![](https://support.huaweicloud.com/dds_faq/public_sys-resources/caution_3.0-zh-cn.png)
  - 如果密码未与mechanisms一起指定，则只能将mechanisms更新为用户当前SCRAM机制的子集。
  
  - 如果密码与mechanisms一起指定，则可以指定任何受支持的SCRAM机制。
  
  - 对于SCRAM-SHA-256，passwordDigestor必须是默认值 "server"。
   
  ```
  db.updateUser(
     "reportUser256",
     {
       mechanisms: [ "SCRAM-SHA-256" ]
     }
  )
  ```
  reporting数据库中的用户reportUser256，经过更新后信息如下：
  ```
  {
  "_id" : "reporting.reportUser256",
  "token" : NumberLong("2827251846225877395"),
  "user" : "reportUser256",
  "db" : "reporting",
  "roles" : [ ],
  "mechanisms" : [
  "SCRAM-SHA-256"
  ]
  }
  ```
  
 
