# [AggregateCommand](AggregateCommand).cond(value: any): Object

> 支持端：[小程序 2.7.4](/miniprogram/dev/framework/client-lib/version.md), [云函数 0.8.1](/miniprogram/dev/wxcloudservice/wxcloud/reference/changelog-server-sdk), [Web](/miniprogram/dev/wxcloudservice/wxcloud/reference/changelog-web-sdk)

聚合操作符。计算布尔表达式，返回指定的两个值其中之一。

## 参数

### value: any

## 返回值

### Object

## API 说明

`cond` 的使用形式如下：

```javascript
cond({ if: , then: , else:   })
```

或者：

```javascript
cond([ , ,  ])
```

两种形式中，三个参数（`if`、`then`、`else`）都是必须的。

如果布尔表达式为真，那么 `$cond` 将会返回 `<真值>`，否则会返回 `<假值>`

## 示例代码

假设集合 `items` 的记录如下：

```json
{ "_id": "0", "name": "item-a", "amount": 100 }
{ "_id": "1", "name": "item-b", "amount": 200 }
{ "_id": "2", "name": "item-c", "amount": 300 }
```

我们可以使用 `cond`，根据 `amount` 字段，来生成新的字段 `discount`：

```javascript
const $ = db.command.aggregate
db.collection('items').aggregate()
  .project({
    name: 1,
    discount: $.cond({
        if: $.gte(['$amount', 200]),
        then: 0.7,
        else: 0.9
    })
  })
  .end()
```

输出如下：

```json
{ "_id": "0", "name": "item-a", "discount": 0.9 }
{ "_id": "1", "name": "item-b", "discount": 0.7 }
{ "_id": "2", "name": "item-c", "discount": 0.7 }
```
