Skip to content

Latest commit

 

History

History
164 lines (122 loc) · 3.57 KB

File metadata and controls

164 lines (122 loc) · 3.57 KB

Pagination

When initializing the service you can set the following pagination options in the paginate object:

  • default - Sets the default number of items
  • max - Sets the maximum allowed number of items per page (even if the $limit query parameter is set higher)

When paginate is set, find will, instead of an Array, return an object in the follow form:

{
  "total": "<total number of records>",
  "limit": "<max number of items per page>",
  "count": "<returned number of items>",
  "skip": "<number of skipped items>",
  "data": [/* data */]
}

Query Parameters

The find API allows the use of $limit, $skip, $sort, and $select in the query. These special parameters can be passed directly inside the query object:

// Find all recipes that include salt, limit to 10, only include name field.
{"ingredients":"salt", "$limit":10, "$select": { "name" :1 } } // JSON

GET /?ingredients=salt&$limit=10&$select[name]=1 // HTTP

As a result of allowing these to be put directly into the query string, you won't want to use $limit, $skip, $sort, or $select as the name of fields in your document schema.

$limit

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

// Retrieves the first two records found where age is 37.
query: {
  age: 37,
  $limit: 2
}

$skip

$skip will skip the specified number of results:

// Retrieves all except the first two records found where age is 37.
query: {
  age: 37,
  $skip: 2
}

$sort

$sort will sort based on the object you provide:

// Retrieves all where age is 37, sorted ascending alphabetically by name.
query: {
  age: 37,
  $sort: { name: 1 }
}

// Retrieves all where age is 37, sorted descending alphabetically by name.
query: {
  age: 37,
  $sort: { name: -1}
}

$select

$select support in a query allows you to pick which fields to include or exclude in the results. Note: you can use the include syntax or the exclude syntax, not both together. See the section on Projections in the NeDB docs.

// Only retrieve name.
query: {
  name: 'Alice',
  $select: {'name': 1}
}

// Retrieve everything except age.
query: {
  name: 'Alice',
  $select: {'age': 0}
}

Filter criteria

In addition to sorting and pagination, properties can also be filtered by criteria. Standard criteria can just be added to the query. For example, the following find all users with the name Alice:

query: {
  name: 'Alice'
}

Additionally, the following advanced criteria are supported for each property.

$in, $nin

Find all records where the property does ($in) or does not ($nin) contain the given values. For example, the following query finds every user with the name of Alice or Bob:

query: {
  name: {
    $in: ['Alice', 'Bob']
  }
}

$lt, $lte

Find all records where the value is less ($lt) or less and equal ($lte) to a given value. The following query retrieves all users 25 or younger:

query: {
  age: {
    $lte: 25
  }
}

$gt, $gte

Find all records where the value is more ($gt) or more and equal ($gte) to a given value. The following query retrieves all users older than 25:

query: {
  age: {
    $gt: 25
  }
}

$ne

Find all records that do not contain the given property value, for example anybody not age 25:

query: {
  age: {
    $ne: 25
  }
}

$or

Find all records that match any of the given objects. For example, find all users name Bob or Alice:

query: {
  $or: [
    { name: 'Alice' },
    { name: 'Bob' }
  ]
}