# [AggregateCommand](AggregateCommand).avg(value: [Expression](/minigame/dev/wxcloud/reference-sdk-api/database/aggregate/Expression)<number>): Object

> 支持端：[小程序 2.7.4](/miniprogram/dev/framework/client-lib/version.md), [云函数 0.8.1](/minigame/dev/wxcloud/reference/changelog-server-sdk), [Web](/minigame/dev/wxcloud/reference/changelog-web-sdk)

聚合操作符。返回一组集合中，指定字段对应数据的平均值。

## 参数

### value: [Expression](/minigame/dev/wxcloud/reference-sdk-api/database/aggregate/Expression)<number>

number

## 返回值

### Object

## API 说明

`avg` 的语法如下：

```javascript
db.command.aggregate.avg()
```

`avg` 传入的值除了数字常量外，也可以是任何最终解析成一个数字的表达式。它会忽略非数字值。

## 示例代码

假设集合 `students` 的记录如下：

```json
{ "group": "a", "name": "stu1", "score": 84 }
{ "group": "a", "name": "stu2", "score": 96 }
{ "group": "b", "name": "stu3", "score": 80 }
{ "group": "b", "name": "stu4", "score": 100 }
```

借助 `avg` 可以计算所有记录的 `score` 的平均值：

```javascript
const $ = db.command.aggregate
db
  .collection('students')
  .aggregate()
  .group({
    _id: null,
    average: $.avg('$score')
  })
  .end()
```

返回的结果如下：

```json
{ "_id": null, "average": 90 }
```
