Querying

All official database adapters support a common way for querying, sorting, limiting and selecting find method calls as part of params.query. Querying also applies update, patch and remove method calls if the id is set to null.

Important: When used via REST URLs all query values are strings. Depending on the service the values in params.query might have to be converted to the right type in a before hook.

Equality

All fields that do not contain special query parameters are compared directly for equality.

  1. // Find all unread messages in room #2
  2. app.service('messages').find({
  3. query: {
  4. read: false,
  5. roomId: 2
  6. }
  7. });
  1. GET /messages?read=false&roomId=2

$limit

$limit will return only the number of results you specify:

  1. // Retrieves the first two unread messages
  2. app.service('messages').find({
  3. query: {
  4. $limit: 2,
  5. read: false
  6. }
  7. });
  1. GET /messages?$limit=2&read=false

Pro tip: With pagination enabled, to just get the number of available records set $limit to 0. This will only run a (fast) counting query against the database and return a page object with the total and an empty data array.

$skip

$skip will skip the specified number of results:

  1. // Retrieves the next two unread messages
  2. app.service('messages').find({
  3. query: {
  4. $limit: 2,
  5. $skip: 2,
  6. read: false
  7. }
  8. });
  1. GET /messages?$limit=2&$skip=2&read=false

$sort

$sort will sort based on the object you provide. It can contain a list of properties by which to sort mapped to the order (1 ascending, -1 descending).

  1. // Find the 10 newest messages
  2. app.service('messages').find({
  3. query: {
  4. $limit: 10,
  5. $sort: {
  6. createdAt: -1
  7. }
  8. }
  9. });
  1. /messages?$limit=10&$sort[createdAt]=-1

$select

$select allows to pick which fields to include in the result. This will work for any service method.

  1. // Only return the `text` and `userId` field in a message
  2. app.service('messages').find({
  3. query: {
  4. $select: [ 'text', 'userId' ]
  5. }
  6. });
  7. app.service('messages').get(1, {
  8. query: {
  9. $select: [ 'text' ]
  10. }
  11. });
  1. GET /messages?$select[]=text&$select[]=userId
  2. GET /messages/1?$select[]=text

$in, $nin

Find all records where the property does ($in) or does not ($nin) match any of the given values.

  1. // Find all messages in room 2 or 5
  2. app.service('messages').find({
  3. query: {
  4. roomId: {
  5. $in: [ 2, 5 ]
  6. }
  7. }
  8. });
  1. GET /messages?roomId[$in]=2&roomId[$in]=5

$lt, $lte

Find all records where the value is less ($lt) or less and equal ($lte) to a given value.

  1. // Find all messages older than a day
  2. const DAY_MS = 24 * 60 * 60 * 1000;
  3. app.service('messages').find({
  4. query: {
  5. createdAt: {
  6. $lt: new Date().getTime() - DAY_MS
  7. }
  8. }
  9. });
  1. GET /messages?createdAt[$lt]=1479664146607

$gt, $gte

Find all records where the value is more ($gt) or more and equal ($gte) to a given value.

  1. // Find all messages within the last day
  2. const DAY_MS = 24 * 60 * 60 * 1000;
  3. app.service('messages').find({
  4. query: {
  5. createdAt: {
  6. $gt: new Date().getTime() - DAY_MS
  7. }
  8. }
  9. });
  1. GET /messages?createdAt[$gt]=1479664146607

$ne

Find all records that do not equal the given property value.

  1. // Find all messages that are not marked as archived
  2. app.service('messages').find({
  3. query: {
  4. archived: {
  5. $ne: true
  6. }
  7. }
  8. });
  1. GET /messages?archived[$ne]=true

$or

Find all records that match any of the given criteria.

  1. // Find all messages that are not marked as archived
  2. // or any message from room 2
  3. app.service('messages').find({
  4. query: {
  5. $or: [
  6. { archived: { $ne: true } },
  7. { roomId: 2 }
  8. ]
  9. }
  10. });
  1. GET /messages?$or[0][archived][$ne]=true&$or[1][roomId]=2

Searching is not part of the common querying syntax since it is very specific to the database you are using. Many databases already support their own search syntax:

For further discussions see this issue.