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]

MongoDB Java Driver notes

A few notes on using the MongoDB Java Driver API:

Getting a connection:

MongoClient client = new MongoClient("localhost", 27017);
DB db = client.getDB("test");

Get a collection from connected db:

DBCollection collection = db.getCollection("example");

 

Find all docs in collection and iterate through results:

DBCursor c = collection.find();
try {
   while(c.hasNext()) {
       System.out.println(c.next());
   }
} finally {
   c.close();
}

 

Simple findOne query, matching a doc with properyname = value:

DBObject result = collection.findOne(new BasicDBObject("propertyname", "value"));

 

Find all, sort on property ‘example’ ascending (1=asc, -1=desc), and limit to 10 results:

DBCursor c = collection.find()
    .sort(new BasicDBObject("example", 1))
    .limit(10);

 

Serialize results of a cursor to JSON (tip from here):

JSON json = new JSON();
String jsonString = json.serialize(c);