Gives some methods that can parse query string parameters into a set of criterias that Mongoid can use to perform actual queries in MongoDB databases for a given model.
This is a gem, so you can install it by:
sudo gem install mongoid_query_string_interface
Or, if you are using rails, put this in your Gemfile:
To use it, just extend Mongoid::QueryStringInterface in your document model:
class Document include Mongoid::Document extend Mongoid::QueryInterfaceString # ... add fields here end
Then, in your controllers put:
def index @documents = Document.filter_by(params) # ... do something like render a HTML template or a XML/JSON view of documents end
That's it! Now you can do something like this:
This would get all documents which have the tags 'ruby', 'rails' and 'mongo', and that don't have both 'sql' and 'java' tags, and that were updated after 2010-01-01, ordered by descending created_at. It will return 10 elements per page, and the 3rd page of results.
You could even query for embedded documents, and use any of the Mongodb conditional operators:
Which would get all documents that have been commented by 'Shrek'. Basically, any valid path that you can use in a Mongoid::Criteria can be used here, and you can also append any of the Mongodb conditional operators.
You can sort results by passing a order_by parameter this:
Which is the same as:
To order by more than one field:
Check the specs for more use cases.
- Vicente Mundim: vicente.mundim at gmail dot com
- Wandenberg Peixoto: wandenberg at gmail dot com