Introduction to MongoDB NoSQL database for SQL Developers – Part 3

In the last post I outlined some basic MongoDB CRUD commands and their relationship/mapping to Structured Query Language. In this post I will go over some key commands and their variables used for data extraction.

Read operations, or queries, retrieve data stored in the database. In MongoDB, queries select documents from a single collection. Queries specify criteria, or conditions, that identify the documents that MongoDB returns to the clients. A query may include a projection that specifies the fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.

For query operations, MongoDB provide a db.collection.find() method. The method accepts both the query criteria and projections and returns a cursor to the matching documents. You can optionally modify the query to impose limits, skips, and sort orders. The following diagram highlights the components of a MongoDB query operation.

MongoDB-part3-crud-annotated-findThe next diagram shows the same query in SQL.

MongoDB-part3-crud-annotated-SQL-select

MongoDB queries exhibit the following behavior:

  • All queries in MongoDB address a single collection.
  • You can modify the query to impose limits, skips, and sort orders.
  • The order of documents returned by a query is not defined and is not defined unless you specify a sort().
  • Operations that modify existing documents (i.e. updates) use the same query syntax as queries to select documents to update.
  • In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries.

MongoDB provides a db.collection.findOne() method as a special case of find() that returns a single document. Consider the following diagram of the query process that specifies a query criteria and a sort modifier.

MongoDB-part3-crud-query-stages

In the diagram, the query selects documents from the users collection. Using a query selection operator to define the conditions for matching documents, the query selects documents that have age greater than (i.e. $gt) 18. Then the sort() modifier sorts the results by age in ascending order.

Queries in MongoDB return all fields in all matching documents by default. To limit the amount of data that MongoDB sends to applications, include a projection in the queries. By projecting results with a subset of fields, applications reduce their network overhead and processing requirements. Projections, which are the the second argument to the find() method, may either specify a list of fields to return or list fields to exclude in the result documents. Consider the following diagram of the query process that specifies a query criteria and a projection.

MongoDB-part3-crud-query-w-projection-stages

In the above diagram, the query selects from the users collection. The criteria matches the documents that have age equal to 18. Then the projection specifies that only the name field should return in the matching documents.

Naturally, MongoDB also supports other query constructs which are used to achieve further functionality for data extraction and retrieval e.g. grouping, case statements, ordering etc. Below is a graphical depiction of the sample SQL statements and their corresponding equivalent in the syntax used by MongoDB.

MongoDB-part3-MySQL-to-MongoDB-mapping

In the next post I will dive deeper into MongoDB’s query syntax and introduce a couple of handy tools which can be used for basic administrative and data retrieval tasks.

http://scuttle.org/bookmarks.php/pass?action=add

Tags: , ,

This entry was posted on Thursday, January 30th, 2014 at 12:59 am and is filed under NoSQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply