# updateIndex

> 本接口应在服务器端调用，详细说明参见[服务端API](/miniprogram/dev/server/getting_started/api_signature.md)。

变更数据库索引

### 请求地址
```
POST https://api.weixin.qq.com/tcb/updateindex?access_token=ACCESS_TOKEN
```

### 请求参数

| 属性 | 类型 | 默认值 | 必填 | 说明 |
| ---- | ---- | ------ | -------- | ---- |
| access_token / cloudbase_access_token | string |  | 是 | [接口调用凭证](/minigame/dev/wxcloud/reference-http-api/functions/invokeCloudFunction) |
| env | string |  | 是 | 云环境ID |
| collection_name | string |  | 是 | 集合名称 |
| create_indexes | Array.<Object> |  | 是 | 新增索引 |
| drop_indexes | Array.<Object> |  | 是 | 删除索引 |

**create_indexes 的结构**

| 属性 | 类型 | 默认值 | 必填 | 说明 |
| ---- | ---- | ------ | -------- | ---- |
| name | string |  | 是 | 索引名 |
| unique | boolean |  | 是 | 是否唯一 |
| keys | Array.<Object> |  | 是 | 索引字段 |

**keys 的结构**

| 属性 | 类型 | 默认值 | 必填 | 说明 |
| ---- | ---- | ------ | -------- | ---- |
| name | string |  | 是 | 字段名 |
| direction | string |  | 是 | 字段排序 |

**direction 的合法值**

| 值 | 说明 | 最低版本 |
| -- | ---- | ---- |
| "1" | 升序 |  |
| "-1" | 降序 |  |
| "2dsphere" | 地理位置 |  |

**drop_indexes 的结构**

| 属性 | 类型 | 默认值 | 必填 | 说明 |
| ---- | ---- | ------ | -------- | ---- |
| name | string |  | 是 | 索引名 |

### 返回值

### Object

返回的 JSON 数据包

| 属性 | 类型 | 说明 |
| ---- | ---- | ---- |
| errcode  | number | 错误码 |
| errmsg  | string | 错误信息 |

**errcode 的合法值**

| 值 | 说明 | 最低版本 |
| -- | ---- | ---- |
| 0 | 请求成功 |  |
| -1 | 系统错误 |  |
| -1000 | 系统错误 |  |
| 40014 | AccessToken 不合法 |  |
| 40097 | 请求参数错误 |  |
| 40101 | 缺少必填参数 |  |
| 41001 | 缺少AccessToken |  |
| 42001 | AccessToken过期 |  |
| 43002 | HTTP METHOD 错误 |  |
| 44002 | POST BODY 为空 |  |
| 47001 | POST BODY 格式错误 |  |
| 85088 | 该APP未开通云开发 |  |
| 其他错误码 | [云开发错误码](/minigame/dev/wxcloud/reference/errcode) |  |

### 请求数据示例

```json
{
  "env": "test2-4a89da",
  "collection_name": "counters",
  "create_indexes": [
    {
      "name":"add_index",
      "unique": true,
      "keys": [
        {
          "name": "test",
          "direction": "2dsphere"
        }
        ]
    }
    ],
  "drop_indexes": [
    {
      "name":"del_index"
    }
    ]
}
```

### 返回数据示例

```json
{
    "errcode": 0,
    "errmsg": "ok",
}
```
