Database Stats In Your Hand with Numerous & Compose


Unfortunately, the Numerous service ceased to be in May 2016. This article remains for historical completeness.

There are some database numbers you need, some you just need to watch and some you need to watch on your Apple Watch, or iPhone or Android smartphone. The numbers in question for us were database statistics and results which we wanted stripped down to their essential numbery-ness and pushed out to various devices... and we didn't want to go down the iOS/Android notifications rabbit hole with custom written applications because we wanted flexibility to experiment.

Services like Zapier and IFTTT didn't fit with what we were doing because they are either overtly document-centric or event-triggered. That's when we found Numerous which creates device-sized dashboards of numeric values. It already integrates with many services which generate numeric information, and can pull in associates comments about those values. But let's show rather than tell and connect up a Compose MongoDB statistic to Numerous.

Making the numbers

The first stop for using Numerous is to download the app for your device. There's links to the various stores on the front page of the site, and here, to the iOS App Store and Android Google Play Store . Download the app to your device and go through the signup process.

You'll find yourself viewing your new empty Numerous dashboard, with an empty square and a plus enticing you to create a new item on it. If you press on it, you can go off and have a play with the many standard Numerous options. Go on. We'll still be here when you get back...

Ok, on with the task at hand. Back to the Numerous dashboard and press on the empty Plus button and scroll through the list of options till you find "Create Your Own". The setup screen will now appear.

Touch on "Give your number a title" and put in "MongoDB Count". You can add your own description or photo at this point. If you don't want people to see your number, remember to touch the eye on the lower right so it reads "Unlisted". Finally touch "Create" and you'll get this...

You'll now be able to choose what notifications you'll get. Select "Value Updates" and then touch on "Ok". Return to the dashboard on your device and you'll have a number with no updates.

We popped an image into the metric's backdrop just to brighten it up a bit. Tapping on the button will open it full screen to display the full image and just the number at the top of the screen and the history of the number below. The next stage in this process is to create some updates...

Creating the updates

For this part, we'll be writing a small Ruby application, partly because there's already a Ruby client library for Numerous. There's also a client library for Python with Node.js and PHP marked as coming soon, but we're familiar with Ruby. The client library installs as a RubyGem with gem install numerousapp. You'll also want to install the MongoDB Ruby driver ('gem install mongo' and 'gem install bson-ext'). Now it's time to create our Ruby program. First up, let's include those gems...

require 'numerousapp'  
require 'mongo'  

Now, we need two values from the Numerous app, one is the API key for your Numerous user. You can get that by selecting the 'hamburger' menu in the top left, selecting "Settings", then selecting "Developer Info". It'll display the API Key and let you copy it so you can, for example, mail it to yourself. Remember, you'll need to keep it secret. We'll include it in the program like so...


Next up is the metric key. This is a pointer to that Number we created earlier in the Numerous app. You find that by going back to the dashboard and tapping to open up the number. This time, a hamburger menu in the top right needs to be touched to display the number specific menu. There's lots of things that can be changed but at the bottom is "Developer Info" touch that and you'll be shown the "Metric ID". We'll include that id in our app like this:


Now we want to point to our database. For this we can use the Replicaset URL that Compose MongoDB databases show you in the Database -> Admin section of the Dashboard. You may want to create a read-only user for the app to use to reduce exposure. We'll include it here like so:


With the credentials all gathered, we can get on with some actual work by opening up the Numerous API and connecting up to the button.  

We're going to be periodically polling the database for our numbers so we want a nice simple way to do this. The following function is just that, from the O'Reilly Ruby Cookbook:

def every_n_seconds(n)  
  loop do
    before =
    interval = n-(
    sleep(interval) if interval > 0

Ok. Protip time: By default, the MongoDB Ruby driver produces a lot of debug output. You are most likely not interested in that till it breaks, so mute it with:

Mongo::Logger.logger.level = 1  

And wake it only when needed. Now we can connect to MongoDB and start our timed task:  
every_n_seconds(10) do  

Every ten seconds, we are going to collect the statistics of a collection in our database. In this case it's called 'names'.

  statdoc=client.command(:collstats => 'names').documents[0]

And then we are going to write the 'count' field from those stats to our metric. Notice the onlyIf:true in the metric.write() parameters. That says it should only update if there's a change. If there isn't a change, it'll throw a NumerousMetricConflictError which can be caught and, in this case ignored. The error being thrown does allow a developer to react to a changing or unchanging metric. That should explain why it's wrapped with a begin...rescue...end anyway.

  rescue NumerousMetricConflictError

And that's it. Now if we run our application, the phone app should notify us that the value has changed.

In this case, we already had a 1000 documents in our database and added three, one at a time giving us the history you see below the number. As each update occurs, Numerous will log it. And notify you. Yes, you can control the notifications to only happen when thresholds or certain percentages change. And after a couple of updates, don't forget to rotate your phone and see a graph of the data too.

Once the value is pushed to Numerous, you get to use all Numerous's capabilities.

Beyond the simple update

Those capabilities include being able to comment in the stream or add likes to the number itself. The Numerous API also lets you add comments so you could add information about the statistics you are gathering. We made the statistic as simple as possible with the count of the document, but you could sample the results of an aggregation or count high scoring players in a game. If you want more users to be able to see particular numbers and their associated streams, you can make the stream public and they can subscribe to it, or keep it unlisted and only share with colleagues and friends. The best part is it's all handled by Numerous's framework so you don't have to touch your own code and

Our example app is just one example, but it could be built out to gather a selection of values from a database and send them to a range of different Numerous numbers. If you have a number you want to get to people's phones, Numerous does seem to take care of the job very nicely.

The code in this article is also available in the Compose Examples repository - we encourage you to check it out and welcome pull requests for improvements.

Dj Walker-Morgan
Dj Walker-Morgan was Compose's resident Content Curator, and has been both a developer and writer since Apples came in II flavors and Commodores had Pets. Love this article? Head over to Dj Walker-Morgan’s author page to keep reading.

Conquer the Data Layer

Spend your time developing apps, not managing databases.