# 数据库聚合

[调试诊断](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_tools)

> 接口应在服务器端调用，不可在前端（小程序、网页、APP等）直接调用，具体可参考[接口调用指南](/doc/oplatform/developers/dev/guide.md)。

接口英文名：dbAggregate

通过本接口可以数据库聚合，使用过程中如遇到问题，可在[开放平台服务商专区](https://developers.weixin.qq.com/community/minihome/mixflow/1355698687267438595)发帖交流。

## 1. 调用方式

### HTTPS 调用

```bash
POST https://api.weixin.qq.com/componenttcb/dbaggregate?access_token=ACCESS_TOKEN
```

### 云调用

- 本接口不支持云调用。

### 第三方调用

- 本接口仅支持第三方平台使用 [component_access_token](/doc/oplatform/developers/dev/ComponentAccessToken.md) 自己调用。

## 2. 请求参数

### 查询参数 `Query String Parameters`

```json
[
  {
    "参数名": "access_token",
    "类型": "string",
    "必填": "是",
    "说明": "接口调用凭证，可使用 [component_access_token](/doc/oplatform/openApi/ticket-token/api_getcomponentaccesstoken)"
  }
]
```

### 请求体 `Request Payload`

```json
[
  {
    "参数名": "env",
    "类型": "string",
    "必填": "是",
    "说明": "云环境ID"
  },
  {
    "参数名": "query",
    "类型": "string",
    "必填": "是",
    "说明": "数据库操作语句"
  }
]
```

## 3. 返回参数

### 返回体 `Response Payload`

```json
[
  {
    "参数名": "errcode",
    "类型": "number",
    "说明": "错误码"
  },
  {
    "参数名": "errmsg",
    "类型": "string",
    "说明": "错误信息"
  },
  {
    "参数名": "data",
    "类型": "array",
    "说明": "记录数组"
  }
]
```

## 4. 注意事项

- 数据库操作语句说明：数据库操作语句语法与数据库 API相同， 聚合操作参考[聚合文档](/miniprogram/dev/wxcloudservice/wxcloud/guide/database/aggregation/aggregation.md)

## 5. 代码示例

请求示例

```json
{
  "env": "test2-4a89da",
  "query": "db.collection(\"test_collection\").aggregate().match({tags:\"cloud\"}).limit(10).end()"
}
```

返回示例

```json
{
  "errcode": 0,
  "errmsg": "ok",
  "data": [
    "{\"_id\":\"f77e039f-f1cf-4aa8-bd59-16cbaa91e6ea\",\"location\":{\"type\":\"Point\",\"coordinates\":[{\"$numberDouble\":\"113.0\"},{\"$numberDouble\":\"23.0\"}]},\"done\":false,\"description\":\"learn cloud database\",\"due\":\"2019-09-09\",\"tags\":[\"cloud\",\"database\"]}",
    "{\"_id\":\"6bb88938-49ea-42b6-a6f5-ce408970cfc6\",\"due\":\"2019-09-09\",\"tags\":[\"cloud\",\"database\"],\"location\":{\"type\":\"Point\",\"coordinates\":[{\"$numberDouble\":\"113.0\"},{\"$numberDouble\":\"23.0\"}]},\"done\":false,\"description\":\"学习 cloud database\"}",
    "{\"_id\":\"51f4f67e-a6a1-4c3e-a50f-827380b8da86\",\"description\":\"学习 cloud database\",\"due\":\"2019-09-09\",\"tags\":[\"cloud\",\"database\"],\"location\":{\"coordinates\":[{\"$numberDouble\":\"113.0\"},{\"$numberDouble\":\"23.0\"}],\"type\":\"Point\"},\"done\":false}",
    "{\"_id\":\"ee1d69da-b7ec-4e7a-bc1f-2fae31da4ce0\",\"tags\":[\"cloud\",\"database\"],\"location\":{\"type\":\"Point\",\"coordinates\":[{\"$numberDouble\":\"113.0\"},{\"$numberDouble\":\"23.0\"}]},\"done\":false,\"description\":\"学习 cloud database\",\"due\":\"2019-09-09\"}"
  ]
}
```

## 6. 错误码

以下是本接口的错误码列表，其他错误码可参考 [通用错误码](/doc/oplatform/developers/errCode/README.md)；调用接口遇到报错，可使用官方提供的 [API 诊断工具](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_errcode) 辅助定位和分析问题。

```json
[
  {
    "错误码": "-1000",
    "错误描述": "system error",
    "解决方案": "系统错误"
  },
  {
    "错误码": "-1",
    "错误描述": "system error",
    "解决方案": "系统繁忙，此时请开发者稍候再试"
  },
  {
    "错误码": "0",
    "错误描述": "ok",
    "解决方案": "ok"
  },
  {
    "错误码": "40014",
    "错误描述": "invalid access_token",
    "解决方案": "不合法的 access_token ，请开发者认真比对 access_token 的有效性（如是否过期），或查看是否正在为恰当的公众号调用接口"
  },
  {
    "错误码": "40097",
    "错误描述": "invalid args",
    "解决方案": "参数错误"
  },
  {
    "错误码": "40101",
    "错误描述": "missing parameter",
    "解决方案": "缺少必填参数"
  },
  {
    "错误码": "41001",
    "错误描述": "access_token missing",
    "解决方案": "缺少 access_token 参数"
  },
  {
    "错误码": "42001",
    "错误描述": "access_token expired",
    "解决方案": "access_token 超时，请检查 access_token 的有效期，请参考基础支持 - 获取 access_token 中，对 access_token 的详细机制说明"
  },
  {
    "错误码": "43002",
    "错误描述": "require POST method",
    "解决方案": "需要 POST 请求"
  },
  {
    "错误码": "44002",
    "错误描述": "empty post data",
    "解决方案": "POST 的数据包为空"
  },
  {
    "错误码": "47001",
    "错误描述": "data format error",
    "解决方案": "解析 JSON/XML 内容错误;post 数据中参数缺失;检查修正后重试。"
  },
  {
    "错误码": "85088",
    "错误描述": "no qbase privilege",
    "解决方案": "该APP未开通云开发"
  }
]
```

## 7. 适用范围

本接口支持「第三方平台」账号类型代调用，权限集请参考「调用方式」部分。其他账号类型如无特殊说明，均不可调用。
