Good tutorial on getting started with using Jasmine for unit testing your JavaScript code:
http://code.tutsplus.com/tutorials/testing-your-javascript-with-jasmine–net-21229
Articles, notes and random thoughts on Software Development and Technology
Good tutorial on getting started with using Jasmine for unit testing your JavaScript code:
http://code.tutsplus.com/tutorials/testing-your-javascript-with-jasmine–net-21229
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.
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]
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
}