|
One of MongoDB's best capabilities is its support for dynamic (ad hoc) queries. Systems that support dynamic queries don't require any special indexing to find data; users can find data using any criteria. For relational databases, dynamic queries are the norm. If you're moving to MongoDB from a relational databases, you'll find that many SQL queries translate easily to MongoDB's document-based query language.
Query Expression ObjectsMongoDB supports a number of query objects for fetching data. Queries are expressed as BSON documents which indicate a query pattern. For example, suppose we're using the MongoDB shell and want to return every document in the users collection. Our query would look like this: db.users.find({})
In this case, our selector is an empty document, which matches every document in the collection. Here's a more selective example: db.users.find({'last_name': 'Smith'})
Here our selector will match every document where the last_name attribute is 'Smith.' MongoDB support a wide array of possible document selectors. For more examples, see the MongoDB Tutorial or the section on Advanced Queries. If you're working with MongoDB from a language driver, see the driver docs: Query OptionsField SelectionIn addition to the query expression, MongoDB queries can take some additional arguments. For example, it's possible to request only certain fields be returned. If we just wanted the social security numbers of users with the last name of 'Smith,' then from the shell we could issue this query: // retrieve ssn field for documents where last_name == 'Smith': db.users.find({last_name: 'Smith'}, {'ssn': 1}); // retrieve all fields *except* the thumbnail field, for all documents: db.users.find({}, {thumbnail:0}); Note the _id field is always returned even when not explicitly requested. SortingMongoDB queries can return sorted results. To return all documents and sort by last name in ascending order, we'd query like so: db.users.find({}).sort({last_name: 1});
Skip and LimitMongoDB also supports skip and limit for easy paging. Here we skip the first 20 last names, and limit our result set to 10:
db.users.find().skip(20).limit(10);
db.users.find({}, {}, 10, 20); // same as above, but less clear
slaveOk (Querying Secondaries)
In the mongo shell, if you try reading from a secondary without specifying slaveOk, you will receive the error message
13435 not master and slaveok=false
In the mongo shell, to indicate slaveOk mode, enter the following:
rs.slaveOk(); // enable querying a secondary
db.users.find(...)
By indicating slaveOk, we are declaring "for my usage on this connection I am ok with eventually consistent reads". CursorsDatabase queries, performed with the find() method, technically work by returning a cursor. Cursors are then used to iteratively retrieve all the documents returned by the query. For example, we can iterate over a cursor in the mongo shell like this: > var cur = db.example.find(); > cur.forEach( function(x) { print(tojson(x))}); {"n" : 1 , "_id" : "497ce96f395f2f052a494fd4"} {"n" : 2 , "_id" : "497ce971395f2f052a494fd5"} {"n" : 3 , "_id" : "497ce973395f2f052a494fd6"} > Quick Reference CardDownload the Query and Update Modifier Quick Reference Card More infoThis was just an introduction to querying in Mongo. More information:
See Also |

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.
blog comments powered by Disqus