Find one or more documents by their _id.
Get all of the documents in a query as an array. Works with options or criteria.
Get all of the documents in a query one at a time and pass them to the given block. Works with options or criteria.
Get the first document in a query. Naturally, this makes the most sense when you have also provided a means of sorting.
Get the last document in a query. Naturally, this makes the most sense when you have also provided a means of sorting.
Paginate the query.
Mongo has rich support for dynamic queries. MongoMapper uses Plucky to construct query proxy objects that only retrieve data from Mongo when needed. This allows a query to be composed of several conditions before being evaluated.
where to specify your query criteria.
Sometimes you know you are loading objects for a very specific purpose–maybe to show a few fields on the UI. You can limit the number of fields returned with data filled in as follows:
Note that all the other attributes in your model will be nil (or set to their default value). Therefore, if you call a method that makes use of all the attributes–like
to_json–then keep in mind that the values will be nil, and you will need to emit only those fields:
Instead of returning an array of complete documents, you may want to merely check to see how many exist.
You can choose to sort the documents by various keys, ascending (default) or descending.
You can limit the number of documents returned by the query.
Skip is used to return a list of documents beyond the number that are requested to be skipped.
Mongo supports quite a few conditional operators. You can use these directly in your MongoMapper queries.
MongoMapper also provides shorthand for most of these operators.
See MongoDB’s documentation on conditional operators.
MongoMapper provides 2 methods for destroying/deleting documents and 2 methods for removing/destroying all documents.
Destroy a single or multiple document(s) by providing ID (s) or tne instance of a document
Destroy all or multiple documents from given matching criteria Warning: You can loose all your data if you call this at the wrong time.
Delete document(s) from given ID (s) or the delete an instance of a document. Callbacks are NOT triggered.
Usage is identical to destroy
Delete multiple or all documents. Callbacks are NOT triggered.
Usage is identical to destroy_all