Quick Start: MongoDB in less than 60 seconds #2

 
If you took my advice from the other day and had a look at MongoDB’s excellent Web Shell then you’ve probably tried out a few commands, maybe even worked through a bit of the tutorial. But you’re not only a busy developer, you’re an experienced developer. You know your way around and you don’t want to waste time with superfluous details. You want answers and you want them now. You can handle the truth. In short, what you want is a quick reference guide.

When it comes to quick reference guides I have 2 requirements (apart from the content being half decent):

  1. They must be printable on regular sized (A4) paper.
    This makes PDF my preferred format.
    As a bonus, this also makes them easy to store, search and annotate in Evernote.
  2. They must be readable.
    I’m too old to be squinting at an illegible, 8 point font.

So, bearing those criteria in mind, here are the best of the free MongoDB quick reference guides:

Quick Start: MongoDB in less than 60 seconds #1

 
If you’re keen to try out MongoDB but don’t have the time (< 5 minutes) or the permissions to install it on your desktop or server then the MongoDB Web Shell may just be what you’ve been looking for.

MongoDB Web Shell
MongoDB Web Shell in action

Built to emulate the Shell app that ships with MongoDB, the Web Shell is a simple-to-use, free, web-based tool that gives you access to a subset of the full list of shell commands. Yes, your session (and data) is lost when you leave the site but if all you want is a sandbox where you can quickly and easily try out some commands this the place for you.

Bonus: it also comes with a basic tutorial for beginners and some help text.


More:

Level Up! 8 resources for a developer getting started with MongoDB

 
At the end of my presentation at CFCAMP 2013 I mention a number of resources for ColdFusion developers that can accelerate their understanding and adoption of MongoDB. Below are the 8 most useful of these resources.

If you…

1. Buy only one book

MongoDB: The Definitive Guide (2nd Edition)
Kristina Chodorow
(Oreilly, May 2013)
http://shop.oreilly.com/product/0636920028031.do

2. Read only one ‘getting started’ tutorial

Getting Started with MongoDB by Mathew Setter
Part 1: http://net.tutsplus.com/tutorials/databases/getting-started-with-mongodb
Part 2: http://net.tutsplus.com/tutorials/databases/getting-started-with-mongodb-part-2

These tutorials were written in early 2012 so they don’t cover all of the latest features (such as the Aggregation Framework) but they are still an excellent resource for any experienced developer who just needs a quick introduction to MongoDB CRUD operations.

3. Take only one online course

M101JS: MongoDB for Node.js Developers
MongoDB University
https://education.mongodb.com/courses/10gen/M101JS/2013_October/about

MongoDB Inc (formerly 10Gen) offer a number of excellent, and free, online courses for developers. These typically last 7 weeks with each week covering a different aspect of Mongo. A certificate is awarded on successful completion of the course homework and final exam (currently around 20% of students pass).

There are other developer courses offered using Python or Java but the real focus is on MongoDB so knowledge of the language is not as important as it might appear. Each course gives a brief introduction to the language being used so this is also a good opportunity to get to know Node.js.

4. Follow only one Twitter feed

@mongodbinc

The official Twitter feed of MongoDB, Inc.

5. Join only one Google+ community

MongoDB

6. Subscribe to only one email newsletter

MongoDB News

My own email newsletter, dedicated to bringing developers a comprehensive round-up of the best apps, tools, blog posts, tutorials and resources.

The first issue will be mailed out on Monday, 4th of November, 2013 and then every two weeks thereafter.

7. Use only one Cloud hosted environment for your MongoDB database

MongoHQ

Easy to set up, fast, reliable and with a free tier that is more than suitable for development work and experimenting.

8. Use only one third-party driver to connect ColdFusion to MongoDB

CFMongoDB

This open source driver for ColdFusion is a wrapper to the official MongoDB Inc Java driver and actively maintained.

{MONGODB NEWS}

{MONGODB NEWS} is a new, fortnightly email of curated links for MongoDB developers.

MongoDB News signup page

 
Earlier today I made public the signup page for a new project of mine. {MONGODB NEWS} is an email of curated links that are of interest to developers using the MongoDB datastore. Every two weeks I shall pick some of my favourite tools, blog posts, books and more. The emphasis will be on resources that make the life of the MongoDB developer easier, more productive and more enjoyable.

The first issue is published on Monday, 4th of November, 2013 but you can sign up now to make sure it arrives nice and fresh in your mailbox. All you’ll ever receive is one mail per fortnight, I’ll never spam you and I won’t sell or give your email address to anyone else. Mailing is handled by MailChimp so there is a hassle-free and instant unsubscribe. If you can’t wait until November, or you prefer a more frequent stream of MongoDB info, I also have a Twitter feed (you can see it on the right hand side of this page).

If you know of any interesting MongoDB related items I should consider for the mailing, please let me know via Twitter or email (david@mongodbnews.com).


More:

MongoDB is ideal for structured logging

 
By now, the benefits of stuctured logging (also known as semantic logging) are well known and accepted:

  • It’s flexible
    Logs change over time – data is added and removed. If new data is added in mid-July I’d still like to be able to query the whole years data, January to December, without having to jump through formatting hoops – if my log changes regularly then I’d really prefer to not have to write or update a custom parser to query it every time a change is made.

  • It’s machine and human readable
    Most ‘standard’ logs are simple comma- or tab-delimited rows of data, primarily intended to be human readable. Many tools to read these logs are flakey and difficult to use, especially when the formatting changes. Ideally, a log should be both human readable to allow quick browsing and machine readable for ad-hoc queries using a standard tool.

JSON to the rescue

Unsurprisingly, the de-facto standard format for stuctured logging is JSON. As a universal file format it is supported in almost every active programming language and many standalone tools exist for reading and searching JSON-formatted logs.

Compare simple log file entries that consist of a date and time, a type and an ip address:

2013/01/22 09:15:23,"login","50.61.75.3"
2013/01/22 09:19:11,"logout","50.61.75.3"

Expressed as JSON this might be:

{
"date_and_time":"2013/01/22 09:15:23",
"type":"login",
"ip":"50.61.75.3"
}
{
"date_and_time":"2013/01/22 09:19:11",
"type":"logout",
"ip":"50.61.75.3"
},

Note: Each of the JSON log entries would usually only take a single line, I have only shown them this way for readability.

The general idea with structured logging is that we preserve the rich data fields as we pass them to the logging tool rather than converting them to a simple string. Put simply, structured loggers log objects, not strings. This improves readability and makes querying the data significantly easier and more powerful.

The benefits of logging to MongoDB

Assuming you’re ready to take advantage of structured logging, a great option is to log directly to a MongoDB collection.
The benefits are significant:

  • Simplified centralisation
    Multiple apps can easily share a MongoDB database and write to one or more collections. The database may be local or remote and, as it’s a regular MongoDB datasource, it can easily and dynamically be replicated or even horizontally scaled (sharded).

  • MongoDB speaks JSON but uses less space
    One disadvantage of creating structured logs is that the metadata saved with them inevitably means JSON formatted logs are larger than plain text logs. MongoDB won’t resolve this problem (if it is a problem for you) but it can reduce the impact. Documents stored in a collection are held in Binary JSON (BSON) format, a compressed binary version of JSON. A simple insert statement is all that it takes to log a complex object (including array, embedded documents and much more). For example, our previous log entry could be inserted by simply wrapping the JSON in an insert statement:

    db.mylog.insert({
    “date_and_time”:”2013/01/22 09:19:11″,
    “type”:”logout”,
    “ip”:”50.61.75.3″
    })

    The MongoDB console provides a simple, Javascript-based interface for querying this data. For example, to retrieve the most recent 10 ‘logout’ entries:

    db.mylog.find({“type”:”logout”}).sort({“date_and_time”:-1}).limit(10)

  • MongoDB is schema-less
    MongoDB collections are schema-less, that is they don’t have a predefined structure in the way a relational database table does. This means that it is very simple to alter what we log at a later date without causing problems to existing queries. For example, after many months of logging we realise that we are not capturing everything we need – we want to add two new columns: ‘browser’ and ‘browser_version’. All we need do is change our insert to include the new fields:

    db.mylog.insert({
    “date_and_time”:”2013/01/22 09:19:11″,
    “type”:”logout”,
    “ip”:”50.61.75.3″,
    “browser”:”IE”,
    “browser_version”:”10.0.1″
    })

    and to retrieve all log entries with browser set to “IE”:

    db.mylog.find({“browser”:”IE”})

    which, in this case, would return only the one document that matched – earlier documents that don’t contain the browser property are ignored.
    No need to change the reporting scripts to cope with missing data.

  • MongoDB has a rich query language that understands JSON
    As we have seen, the MongoDB console has a rich query language that lets us query our logs with a JSON like syntax. Support exists for filtering (including embedded arrays and documents), sorting, complex aggregation and much more. Logs can finally become rich sources of information without having to write bespoke code.

  • Writes are fast and may be asynchronous
    Writes to collections can be asynchronous – just fire and forget. This means that, in many circumstances, inserting entries into the log can be almost as fast as writing direct to disk.

  • Capped collections may be limited by space or number of documents (or both)
    A capped collection is a collection of documents that is limited to a specific size (specified in bytes) or number of documents.
    When the size or document count is going to be exceeded the oldest documents are removed from the collection to make way for the newest. This makes it easy to, for example, only store the last few thousand log entries or set the maximum size to 5Mb and never worry about runaway code filling up a production hard drive.

In short, MongoDB is a great solution for storing and retrieving structured logs.

In a future post I’ll cover in more detail how to take advantage of capped collections and maximise throughput when logging to MongoDB.


More:

Level Up! at CFCAMP 2013

I shall be speaking at this years CFCAMP to be held in Germering near Munich, Germany on October 14th and 15th this year. The subject is MongoDB and ColdFusion. Here’s the blurb…

Level Up!
Taking your ColdFusion apps to the next level with MongoDB.

The new generation of NoSQL databases offer unprecedented flexibility and ease-of-use when compared to their more traditional, relational rivals.

Discover how adding the powerful features of market-leading NoSQL database MongoDB to your skill set can make your ColdFusion development both more productive and more enjoyable.

My talk is for those ColdFusion developers who just haven’t found the time to delve into exactly what NoSQL in general and MongoDB in particular are all about. A basic understanding of ColdFusion syntax will be useful but no MongoDB experience is necessary.

There are still some CFCAMP tickets available – as always, the sooner you book the less you’ll pay for this great event.

More:

How FourSquare uses MongoDB as their primary datastore

In this 45-minute video, Andrew Erlichson, Vice President of Education at 10gen (the makers of MongoDB) interviews John Hoffman, Manager of the Storage Team at FourSquare, about his company’s use of MongoDB as their primary datastore.

John discusses in detail FourSquare’s evolution from MySQL to Postgres to MongoDB, the challenges that led them to make the transition and goes into some detail about how FourSquare make use of the unique features of MongoDB.

This really is essential viewing for anyone interested in MongoDB and a great insight into what’s involved in managing the data of a high-performance, high usage, web-based app.

As a not totally irrelevant aside, I only recently came across this video as it is made available in Week 7 of the excellent (and free) “MongoDB for Developers” certification training course. For any developer new to MongoDB or just interested in knowing what all the NoSQL/document store excitement is about this is an essential course. Combine it with the “MongoDB for DBAs” course and you’ll come away with a good grasp of what’s involved in switching from a relational to a document-based database.

More: