Major query refactoring and additions

This pull request was originally for grouping, but enough was happening in it outside of that that I decided to split it up.

This pulls queries out into their own namespace and makes a record for them. It also chooses to use the Monger query syntax or aggregation framework depending on whether SELECT aliases are used.