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.

AngularJS Tutorial – notes (1)

Random notes from following through the AngularJS tutorial here.

My first time (several months back) working through part of the tutorial I struggled getting node.js up and running on Windows. Useful thread and comments on this post here.

Installing on Mac OS X, no problems.

Useful Tutorial Steps – Tools Install and Setup

(not intended to be a comprehensive set of steps, just mainly for myself to backtrack and see what I’ve done so far in the tutorial)

Install git (already have)

Clone the tutorial source project:

git clone --depth=14 https://github.com/angular/angular-phonecat.git

Download and install node.js from here.

npm install – install the development tools.Note: the tutorial is not specific where you run this, but you must run this inside the clones angular-phonecat dir.

Useful scripts setup with the tutorial project:

  • npm start : start a local development web-server
  • npm test : start the Karma unit test runner
  • npm run update-webdriver : install the drivers needed by Protractor (run this once, and first before running Protractor)
  • npm run protractor : run the Protractor end to end (E2E) tests

Interesting note: no issue with the default config on Mac OS X and running the dev server on port 8000. This was an issue on my Windows machine and needed to change the default port to something other than 8000.

Errors Starting npm run protractor

All the other scripts ran first time for me, but protractor would not start, giving an error about ELIFECYCLE. Seems similar to this issue. I ran these to steps:

npm update
npm run update-webdriver

but then the error changed to something more verbose, about couldn’t find an .exe file:

util.puts: Use console.log instead
Using ChromeDriver directly...
[launcher] Error: Could not find chromedriver at /Users/kev/angularjs/angular-phonecat/node_modules/protractor/selenium/chromedriver.exe

A quick Google found this issue, and the steps described by jpaljasma fixed the issue:

  • edited package.json in the root of the tutorial project, changed devDependencies.protractor to be “*”, removed node_modules, ran npm install and npm run update-webdriver

Now the e2e tests supplied with the project run as expected.