Search This Blog

Thursday, December 19, 2013

Exercise 2 and 3 - the order of functions

Important note: This article is in relation to online MongoDB course. For more information about the course and other posts describing its content please check my main page here: M101P: MongoDB for Developers course

Importance and order of functions in MongoDB when retrieving data

When running week 1 homework scripts we get following results:


The code is simple. There is only one line of code that is interesting.

iter = collection.find({},limit=1, skip=n).sort('value', direction=1)

There are two methods executed one after another. The first one is find and the second sort. At fist it looks like the sort method is only applied to the results returned by find but this is not truth.

What is actually happening is:
  • The find method returns all documents from the data base (the order of the values is determined by the database what can be read as random; this would be the equivalent of a select * from table statement in SQL)
  • The list is then sorted base on the value. The value represent here one of the document attributes.
  • After the returned documents (i.e. returned rows from table in RDMS data base) are sorted we skip the first 'n' rows and return a single element.

No comments:

Post a Comment