Methods
These are the details for the methods used. They really don't ever need to be called on their own since they're called in the path mappings, but here's the low down anyway.
find
Mapping
HTTP Verb | Path | Mongoose Model Method |
---|---|---|
GET | many_path | find |
Details
find
retrieves as array of documents. There are a number of tweaks added to vanilla mogoose model find
call. The largest change is the object returned: it consists of meta information about the query and the documents.
The keys of the object are meta
and objects
.
meta
The meta object contains the following:
Key | Value |
---|---|
page_size | The number of documents specified by the limit parameter (default is 20) |
count | The total number of documents that satify the query |
next | If there are multiple pages of results, this is the next logical page (default is null ) |
previous | If there are multiple pages of results, this is the page that would precede the current result (default is null ) |
objects
An array of documents that satisfy the query, limited by the page_size
.
Example Output
{
meta: {
page_size: 20,
count: 2,
next: null,
prevous: null
},
objects: [
{
name: "Tim-tom",
affiliations: [ "The Murderous Moppets", "The Pupae Twins" ]
},
{
name: "Kevin",
affiliations: [ "The Murderous Moppets", "The Pupae Twins" ]
}
]
}
Querying
Queries are mapped via the querystring of the request. There are sever ubiquitous options in conjunction with using the model's
properties.
Key | Value |
---|---|
limit | Sets the limit of the number of documents in the result |
skip | Sets the number of documents to be skipped in the result — order is kept by the server as descending by _id |
order | Sets the order of the documents in the result by specifying a model property — prefix with - for descending |
Pagination
Pagination can be achieved by using the limit
and skip
keys. This is what is used for the next
and
previous
keys of the meta information.
Advanced Querying
Documents can be queried using MongoDB Query and Projection Operators.
Example
If you wanted a list of charachters whose ages are between 16 and 20:
/characters?age={"$gte":16,"$lte":20}
(if you're following the trend of Venture Bros. related examples, this would yield Hank, Dean, Triana and Dermott)
Emits
No event emitted.
Returns
A JSON object that consists of meta information and an array of documents.
Status Code
200
find_one
Mapping
HTTP Verb | Path | Mongoose Model Method |
---|---|---|
GET | single_path | findById |
Details
find_one
finds a single document by id.
Emits
No event emitted.
Returns
The document specified in the request parameter.
Status Code
200
create
Mapping
HTTP Verb | Path | Mongoose Model Method |
---|---|---|
POST | many_path | create |
Details
create
creates a new document with the data supplied in the request body.
Emits
emitter
emits an event titled created
along with the new document's id.
Returns
The new doument.
Status Code
201
update
Mapping
HTTP Verb | Path | Mongoose Model Method |
---|---|---|
PUT | single_path | findByIdAndUpdate |
Details
update
finds a document by id, stores its current value, then executes the update. After that
a diff is performed and then dumped.
Emits
emitter
emits an event titled updated
with an additional object:
The additional object is the result of deep-diff.diff
between the original document and
the new one.
For example (using the node repl):
> diff = require('deep-diff').diff;
> existing = { name: 'Dean Venture', dogs: [ 'Scamp' ] }
{ name: 'Dean Venture', dogs: [ 'Scamp' ] }
> updated = { name: 'Dean Venture' }
{ name: 'Dean Venture' }
> difference = diff(existing, updated);
[ { kind: 'D',
path: [ 'dogs' ],
lhs: [ 'Scamp' ] } ]
For more information about
deep-diff
, see their documentation at
https://github.com/flitbit/diff.
Returns
The new document.
Status Code
202
destroy
Mapping
HTTP Verb | Path | Mongoose Model Method |
---|---|---|
DELETE | single_path | findByIdAndDestroy |
Details
destroy
finds a document by id and destroys it.
Emits
emitter
emits an event titled destroyed
.
Returns
Nothing: an HTTP No Content response.
Status Code
204