MongoDB aggregation query with a $sort

I have an aggregation query to count documents grouped by a property (this is actually part of my http://www.spotviz.info app that I’m working on – the query retrieves counts per Amateur Radio callsign for number of spots uploaded).

By default the result are not in any particular order (that I can see, but maybe I don’t enough enough test data to be able to tell), so I wanted to add a $sort condition.

Here’s the initial aggregation query:

db.Spot.aggregate(
[
{ $group : { _id : {"spotterCallsign" : "$spotter"},
count : {$sum : 1},
firstSpot : {$min : "$spotReceivedTimestamp"},
lastSpot : {$max : "$spotReceivedTimestamp"} }
}
] )

To add the $sort to the aggregation pipeline, just add another document for $sort following the $group, like this:

{ $sort : { "count" : -1 } }

The full query now looks like:

db.Spot.aggregate(
[
{ $group : { _id : {"spotterCallsign" : "$spotter"},
count : {$sum : 1},
firstSpot : {$min : "$spotReceivedTimestamp"},
lastSpot : {$max : "$spotReceivedTimestamp"} }
},
{ $sort : { "count" : -1 } }
] )

Building this query with the Java API is easy, just add another DBObject for the $sort document to the List containing all docs ($group, $sort), in the pipeline:

DBCollection col = db.getCollection("Spot");

// $group
DBObject groupFields = new BasicDBObject("_id", "$spotter");
groupFields.put("firstSpot", new BasicDBObject("$min", "$spotReceivedTimestamp"));
groupFields.put("lastSpot", new BasicDBObject("$max", "$spotReceivedTimestamp"));
groupFields.put("totalSpots", new BasicDBObject("$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields);

List<DBObject> pipeline = Arrays.asList(group,
new BasicDBObject("$sort", new BasicDBObject("totalSpots", -1)));

AggregationOutput output = col.aggregate(pipeline);

AngularJS console errors are links to further details (?!)

I don’t know why I didn’t notice or try this before, but I noticed in the Udemy ‘AngularJS Zero to Hero’ course that the instructor was clicking the links in the console errors. Previously I thought those rather oddly looking random character messages in the console were just not particularly friendly, but now they make more sense.

Maybe this is completely obvious to others, but this is a very neat feature that I had completely not picked up on.  The links are parameterized with details of your specific error, and the error message page in the AngularJS docs gives you more specifics about the error.

For example, if I have a typo in my ng-app:

Clicking on the link in the error message takes you to a detailed page with further details:

Managing web dependencies with bower

Some rough notes on Bower to remember for future usage:

  • bower install name : install dependency ‘name’
  • bower list : list current project dependencies
  • bower search name : search for a module in the repo
  • bower update name : update name to latest
  • bower update name#1.1 : update name to 1.1

Getting started ref here.

Getting Started with AngularJS (pt 4): Form submits and two-way data binding

The trouble with learning something new and then not using it for a while is you tend to forget everything you learned a few weeks back.

So picking up again on learning AngularJS (see my previous posts here), I had completely forgot how to handle form submits using AngularJS.

The key is to use the directive ng-submit=”functionname()” on your form, where functionname() is some function defined in $scope in your controller for this form.

The values in your form are automagically bound to your model in your controller, and you can handle the user action of pressing a submit button to do whatever processing you need.

There’s a great explanation in this article here.