Chinese Documentation : Where过滤器

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

REST API

下面的代码使用等于条件

filter[where][property]=value

filter[where][property][op]=value

可以在REST query中使用stringified JSON format

Node API

Icon

Methods of models in the AngularJS client have a different signature than those of the Node API. For more information, see AngularJS SDK API.

等于条件

{where: {propertyvalue}} 

{where: {property: {opvalue}}}

Where:

  • property 是属性的名字
  • value 是属性的值
  • op 是下面的operators

Examples

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

等于:

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

更多例子见Examples

AND and OR operators

使用AND和OR组合条件。

REST

[where][<and|or>][0]condition1&[where][<and|or>]condition2...
Node API
{where: {<and|or>: [condition1condition2, ...]}}
Where condition1 and condition2 are a filter conditions.

操作符

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

OperatorDescription
and
or或者
gt, gte

大于(gt = greater than); 大于或等于(gte = greater than equal)。这个只对数字和日期有效。

lt, lte小于(lt = less than);小于或等于(lte = less than equal)。这个只对数字和日期有效。
between

当值在指定的两个值之间为真:大于或等于第一个值,并小于或等于第二个值。

inq, ninIn / not in一个数组里面。
near地理位置,返回最近的点。使用limit返回最近的n个点。
neq不等于 (!=)
like, nlike

LIKE / NOT LIKE 一个正则表达式。正则表达式的格式依赖于后端的data source。

例子

等于

返回名字为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

等于:

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"]}}}