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.

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
}

MongoDB usage notes (2)

Continuing prior notes from here.

Drop a database:

use databasename
db.dropDatabase()

List distinct property values across all docs:

db.collectionname.distict("propertyname")

Remove docs in a collection – param is a doc query. If empty doc, removes all docs:

db.collectionname.remove({})

Date range query using $gt and $lt for a range:

db.collectionname.find({ "timestamp" : { "$gte" : ISODate("2014-07-08T19:50"), "$lt" : ISODate("2014-07-08T19:52")  } })

[in progress]