0.1.7 • Published 2 years ago
@nwire/api v0.1.7
API Query
Example
const query = 'status=sent×tamp>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
MongoDB | URI | Example |
---|---|---|
$eq | key=val | type=public |
$gt | key>val | count>5 |
$gte | key>=val | rating>=9.5 |
$lt | key<val | createdAt<2016-01-01 |
$lte | key<=val | score<=-5 |
$ne | key!=val | status!=success |
$in | key=val1,val2 | country=GB,US |
$nin | key!=val1,val2 | lang!=fr,en |
$exists | key | phone |
$exists | !key | !email |
$regex | key=/value/<opts> | email=/@gmail\.com$/i |
$regex | key!=/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 tocountry=GB&country=US
sort=-createdAt,lastName
is equivalent tosort=-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'