Creating test data in MongoDB using node.js

I need to create a fairly large number of test documents in my MongoDB to test some functionality that will be retrieving pages of data, ordered by date.

Rather than doing this by hand, using the Node.js driver for MongoDB makes this pretty easy with only a few lines of code. This is a starting point, in my case I need to add some additional logic to increment a date value and create some other random values on each doc, but this is a rough outline of an approach:

Assuming Node.js already installed, install the MongoDB Node.js driver with:

npm install mongodb

Then the code to install multiple docs in one shot looks something like this:

[code language=”javascript”]

var MongoClient = require(‘mongodb’).MongoClient,
test = require(‘assert’);

var ObjectId = require(‘mongodb’).ObjectID;

MongoClient.connect(‘mongodb://localhost:27017/databasename’, function(err, db) {
test.equal(err, null);

var col = db.collection(‘collectioname’);
var docs = [];

//update this for how many docs to insert

for(i=0; i<10; i++){
docs[i] = {a:i}; // create doc here using json
}

col.insertMany(docs, function(err, r) {
test.equal(err, null);
console.log(r.insertedCount);
db.close();
});
});

[/code]

Run the script with:

node scriptname.js

Additional info in the MongoDB Getting Started Guide for node.js.

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]