Page Contents

where过滤器指定了匹配的where条件,类似于SQL的where。

REST API

下面的代码使用等于条件

filter[where][_property_] = _value_

filter[where][_property_][_op_] = _value_

可以在REST query中使用stringified JSON format

Node API

等于条件

{
  where:  {
    _property_:  _value_
  }
} 

{
  where:  {
    _property_: {
      _op_:  _value_
    }
  }
}
```Where:

*   _property_ 是属性的名字
*   _value_ 是属性的值
*   _op_ 是下面的[operators](6095118.html)

### Examples

`/api/cars?filter[where][carClass]=fullsize`

等于:

`Cars.find({ where: {carClass:'fullsize'} });`

更多例子见[Examples](6095118.html)。

**AND and OR operators**

使用AND和OR组合条件。

**REST**

`[where][<and|or>][0]condition1&[where][<and|or>]_condition2__..._`**Node API**`{where: {<and|or>: [_condition1_, _condition2_, ...]}}`Where _condition1_ and _condition2_ are a filter conditions.

### 操作符

下面的例子列出了where过滤器的所有操作符。

<table>
  <tbody>
    <tr>
      <th>Operator</th>
      <th>Description</th>
    </tr>
    <tr>
      <td>and</td>
      <td>并</td>
    </tr>
    <tr>
      <td>or</td>
      <td>或者</td>
    </tr>
    <tr>
      <td>gt, gte</td>
      <td>
        <p>大于(gt = greater than); 大于或等于(gte = greater than equal)。这个只对数字和日期有效。</p>
      </td>
    </tr>
    <tr>
      <td>lt, lte</td>
      <td>小于(lt = less than);小于或等于(lte = less than equal)。这个只对数字和日期有效。</td>
    </tr>
    <tr>
      <td>between</td>
      <td>
        <p>当值在指定的两个值之间为真:大于或等于第一个值,并小于或等于第二个值。</p>
      </td>
    </tr>
    <tr>
      <td>inq, nin</td>
      <td>In / not in一个数组里面。</td>
    </tr>
    <tr>
      <td>near</td>
      <td>地理位置,返回最近的点。使用limit返回最近的n个点。</td>
    </tr>
    <tr>
      <td>neq</td>
      <td>不等于 (!=)</td>
    </tr>
    <tr>
      <td>like, nlike</td>
      <td>
        <p>LIKE / NOT LIKE 一个正则表达式。正则表达式的格式依赖于后端的data source。</p>
      </td>
    </tr>
  </tbody>
</table>

### 例子

#### 等于

返回名字为M1911的武器:

`/weapons?filter[where][name]=M1911`

#### 大于和小于

ONE_MONTH = 30 * 24 * 60 * 60 * 1000; // Month in milliseconds transaction.find({ where: { userId: user.id, time: {gt: Date.now() - ONE_MONTH} } }


下面的例子返回所有data大于指定值的所有员工:

`/employees?filter[where][date][gt]=2014-04-01T18:30:00.000Z`

等于:

```js
Employees.find({
  where: { 
    date: {
      gt: Date('2014-04-01T18:30:00.000Z')
    }
  }
});

返回3个射程大于900米的武器:

/weapons?filter[where][effectiveRange][gt]=900&filter[limit]=3

返回声音小于10的武器:

/weapons?filter[where][audibleRange][lt]=10

and / or

返回标题为My Post并且内容为Hello的帖子。

Post.find({where: {and: [{title: 'My Post'}, {content: 'Hello'}]}}, 
          function (err, posts) {
            ...
});

等于:

?filter[where][and][0][title]=My%20Post&filter[where][and][1][content]=Hello

使用or查找所有标题等于My Post或者内容等于Hello的帖子

Post.find({where: {or: [{title: 'My Post'}, {content: 'Hello'}]}}, 
          function (err, posts) {
            ...
});

下面的例子更复杂一些

(field1= foo and field2=bar) or field1=morefoo:

{
  "or": {
    "and": [{
      "field1": "foo"
    }, {
      "field2": "bar"
    }],
    "field1": "morefoo"
  }
}

between

between操作符:

filter[where][price][between][0]=0&filter[where][price][between][1]=7

Node API:

Shirts.find({where: {size: {between: [0,7]}}}, function (err, posts) { ... } )

near

下面的例子使用near操作符返回3个离指定坐标最近的位置:

/locations?filter[where][geo][near]=153.536,-28.1&filter[limit]=3

like and nlike

like和nlike(not like)操作符类似于SQL里面的like,使用正则表达式进行匹配。正则表达式的格式依赖于后端的data source。

例子:

Post.find({where: {title: {like: 'M.+st'}}}, function (err, posts) { ... });

nlike的例子:

Post.find({where: {title: {nlike: 'M.+XY'}}}, function (err, posts) {

当使用内存数据库时:

User.find({where: {name: {like: '%St%'}}}, function (err, posts) { ... });
User.find({where: {name: {nlike: 'M%XY'}}}, function (err, posts) { ... });

inq

inq检查指定属性的值是否在数组里面。

{ where: { _property_: { inq: [_val1_, _val2_, ...] } } }

  • property 是属性的值
  • val1, val2, 是数组的值

inq的例子:

Posts.find({where: {id: {inq: [123, 234]}}}, 
  function (err, p){... });

REST:

/medias?filter[where][keywords][inq]=foo&filter[where][keywords][inq]=bar

?filter={"where": {"keywords": {"inq": ["foo", "bar"]}}}