Mongo Query Language

Queries in MongoDB are expressed as JSON (BSON). Usually we think of query object as the equivalent of a SQL "WHERE" clause:

> db.users.find( { x : 3, y : "abc" } ).sort({x:1}); // select * from users where x=3 and y='abc' order by x asc;

However, the MongoDB server actually looks at all the query parameters (ordering, limit, etc.) as a single object. In the above example from the mongo shell, the shell is adding some syntactic sugar for us. Many of the drivers do this too. For example the above query could also be written:

> db.users.find( { $query : { x : 3, y : "abc" }, $orderby : { x : 1 } } );

The possible specifies in the query object are:

  • $query - the evaluation or "where" expression
  • $orderby - sort order desired
  • $hint - hint to query optimizer
  • $explain - if true, return explain plan results instead of query results
  • $snapshot - if true, "snapshot mode"

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE FORUMS: http://groups.google.com/group/mongodb-user. Post tips and clarifications here.

blog comments powered by Disqus