0.1.7 • Published 2 years ago

@nwire/api v0.1.7

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
2 years ago

API Query

Example

const query = 'status=sent&timestamp>2016-01-01&author.firstName=/john/i&limit=100&skip=50&sort=-timestamp&populate=logs&fields=id,logs.ip'
const result = {     filter: {         status: 'sent',         timestamp: { $gt: new Date('Fri Jan 01 2016 01:00:00 GMT+0100 (CET)') },         'author.firstName': /john/i }, sort: { timestamp: -1 }, skip: 50, limit: 100, projection: { id: 1 }, population: [ { path: 'logs', select: { ip: 1 } } ]
}

Supported features

Filtering operators

MongoDBURIExample
$eqkey=valtype=public
$gtkey>valcount>5
$gtekey>=valrating>=9.5
$ltkey<valcreatedAt<2016-01-01
$ltekey<=valscore<=-5
$nekey!=valstatus!=success
$inkey=val1,val2country=GB,US
$ninkey!=val1,val2lang!=fr,en
$existskeyphone
$exists!key!email
$regexkey=/value/<opts>email=/@gmail\.com$/i
$regexkey!=/value/<opts>phone!=/^06/

For more advanced usage ($or, $type, $elemMatch, etc.), pass any MongoDB query filter object as JSON string in the filter query parameter, ie:

'filter={"$or":[{"key1":"value1"},{"key2":"value2"}]}'

Skip / Limit operators

'skip=5&limit=10'

Projection operator

  • It accepts a comma-separated list of fields. Default behavior is to specify fields to return. Use - prefixes to return all fields except some specific fields.
  • Due to a MongoDB limitation, you cannot combine inclusion and exclusion semantics in a single projection with the exception of the _id field.
  • It also accepts JSON string to use more powerful projection operators ($, $elemMatch or $slice)
'fields=id,url'
'fields=-_id,-email'
'fields={"comments":{"$slice":[20,10]}}'

Sort operator

  • It accepts a comma-separated list of fields. Default behavior is to sort in ascending order. Use - prefixes to sort in descending order.
'sort=-points,createdAt'

Population operator

  • It accepts a comma-separated list of fields.
  • It extracts projection on populated documents from the projection object.
'populate=a,b&fields=foo,bar,a.baz'

Keys with multiple values

Any operators which process a list of fields ($in, $nin, sort and projection) can accept a comma-separated string or multiple pairs of key/value:

  • country=GB,US is equivalent to country=GB&country=US
  • sort=-createdAt,lastName is equivalent to sort=-createdAt&sort=lastName

Embedded documents using . notation

Any operators can be applied on deep properties using . notation:

'followers[0].id=123&sort=-metadata.created_at'