MongoDB Shell Functions – Can we customize ?

While working on MongoDB, I accidentally ran a find() count query incorrectly which resulted in something interesting.

While I wanted to execute db.team.find().count(), I ended up hitting enter key a bit too sooner and resultant query in the mongo shell was db.team.find().count  [yeah, without the ()]

While I was expecting an error, the shell actually responded with the function definition of count() method and it goes as below..

 

Query.png

So, above is what exactly happens when we call find().count() in the shell.

So with this, you can start looking into the design and implementation of shell functions and understand the login underneath.

 

Interpreting the query definition ourselves, we can get the count of collection as below as well.

  1. Grab a collection into variable v.
    • var v = db.team.find()
  2. Find the count of v
    • v.count()
  3. use the convertToCountCmd to get the json object
    • var cmd1 = v._convertToCountCmd()
  4. Execute the runReadCommand to get the details of count.
    • db.runReadCommand(cmd1)
  5. db.runReadCommand(cmd1).n
    • Will give the exact count needed.

Below picture depicts the same.

Query2.png Well, Now thats fun..

 

 

Also, Here is how the find() method definition looks like..Query1

One final thought before ending this blog post, will MongoDB allow us to extend the functionality of these methods? For Ex, can i create my own custom Count method using or my own shell function ? At this moment i’m not sure..

Calling all #Mongo experts to answer 🙂

 

 

 

 

Directives in AngularJS

Directives in AngularJS helps in extending the power of HTML.

The important responsibilities of directives are

  1. DOM manipulation [ng-show]
  2. Relieving view events  [ng-click]
  3. Widgets [via custom directives]

 

AngularJS provides a set of build in directives like ng-bind, ng-class etc.

Custom Directives can also be written quite easily. We can either use

  1. Link function of the directive to define how the directive should behave.
  2. Create a Directive Controller and put the logic.

Directives in your View/HTML can be written as:

  1. Element Names or Tags (E)
  2. Attributes (A)
  3. Classes (C)
  4. HTML Comments (M)

(E, A C or M are restriction types)

Note : Angular normalizes an element’s name or tag to map elements and directives. More details can be found here.

 

 

Custom Directive Code Snippet:

 

 

Few important points to note here:

  1. Angular doesn’t complain if there are multiple directives with the same name declared on a page.
  2. While loading the Dynamic DOM, Angular compiles all the directives [including ones with same name as in our examples] but only executes the first directive.
  3. Changing the order of the directive declarations having the same name impacts your output.
  4. Hence its vital to name directives uniquely.

My next post will talk more about

Requiring Directives within another and isolation of scope.

 

Mean Stack

Post#1

MEAN and How..

MEAN is one of the leading full stack java script application development stack which is currently widely used across industry to build large scale DIRTy [Data Intensive Real Time ] applications.

Of course we’ve had LAMP, WAMP and other full stack frameworks during the yester decades. However, MEAN beats them all in many ways and importantly it is Platform independent. Your Code will work on Linux, windows and heck even mac.

The essence of MEAN is Java script and i’ll continue to blog about a few things here for the betterment of the world 😛