For me it seems logical to start my clauses with the where, and narrow them down further, or modify them… In MongoMapper, I find querying rigor is much more “loose” than say a SQL SELECT query that requires things in proper order… I would tend to write my queries in more or less this fashion:
ModelClass.where(some criteria).[sort | order | another where clause | fields | limit].[all | first | paginate]
In addition, it is important to note that MongoMapper returns a query and does not actually perform the query until you add something that needs the results. For example: all
, first
, paginate
, sort
, etc.
2.0.0-p247 :001 > Structure.where(:address => /NJ/).count
=> 22
2.0.0-p247 :002 > Structure.where(:address => /NJ/).limit(2).count
=> 22
2.0.0-p247 :003 > Structure.where(:address => /NJ/).limit(2).all.count
=> 2
More details here: http://technicaldebt.com/mongomapper-query-review/ and for the underlying Plucky query syntax, here: https://github.com/mongomapper/plucky