Testing for an empty JSON document

I have a RESTful service that performs a search and returns the results as JSON, and if there’s no results I return an empty document, {}.

Calling this search from AngularJS with $http.get(), I get the returned JSON result and everything is good if I have a document containing data, but an empty document took a bit more Googling to work out how to detect it.

From this similar question, I don’t want to use jQuery in my AngularJS app if I can avoid it (although doesn’t AngularJS have a “jQuery lite” api that maybe I can use?), so I used this approach instead:

[code]
if(Object.keys(data) == 0) { … }
[/code]

Finding min and max values with MongoDB Aggregation

Aggregation operations in MongoDB allow you to process documents to produce computed results. An example is to search for a min or max value. For documents that look like this:

{
  "_id" : ObjectId("5521ae8c0364d302910ec5eb"),
  "color" : "blue",
  "orderDate" : ISODate("2014-07-08T19:53:00Z")
  //other properties
}

You can find the min/earliest date in the collection for an document with matching color of blue with an aggregation query like this:

db.collectionName.aggregate(
[
{ "$match" : { "color":"blue" } },
{ "$group" : { "_id" : "$color",
first: { "$min" : "$orderDate"},
}
])

Implementing this with the Java Driver API looks like this:


DBObject match = new BasicDBObject("$match", new BasicDBObject("color", "blue"));

//$group
DBObject groupFields = new BasicDBObject( "_id", "$color");
groupFields.put("orderDate", new BasicDBObject( "$min", "$orderDate"));
DBObject group = new BasicDBObject("$group", groupFields);

List<DBObject> pipeline = Arrays.asList(match, group);

AggregationOutput output = col.aggregate(pipeline);
for (DBObject result : output.results()) {
//iterate results, only 1 for $min
}

Simplest AngularJS Service and a Jasmine unit test

The syntax for creating an AnguarJS service is rather alien to me as a Java developer, so I struggled to get this right, despite looking at and trying a number of different examples from different articles and howtos.

I think I’ve got this distilled to a simplest possible example now, so for future reference, this is a bare minimum service:

[code language=”javascript”]

angular.module(‘AppName.services’, [])
.factory(‘exampleService’, function () {

var serviceImpl = {};

//methods on service here

return serviceImpl;
});

[/code]

The syntax for implementing methods on the Service is like this (insert into where the comment is in the above example:

[code]

return {
exampleMethod: function (exampleParam1) {
var result;

//do something here

return result;
}
};

[/code]

A simple Jasmine test for a Service looks like this:

[code]

describe(‘Example service’, function () {
var exampleService;

it("Should be something here", function () {

var $injector = angular.injector([ ‘AppName.services’ ]);

exampleService = $injector.get( ‘exampleService’ );
var result = exampleService.exampleMethod(1);
expect(result).toBe(1);
});

});
[/code]