The role of the DBA in NoSQL

What is the role of the DBA in the rapidly evolving world of NoSQL? A majority of the early NoSQL adoption is in the fast growing world of small and medium companies based on public clouds.  In most of these companies the DBA role does not exist and this has led a lot of people to proclaim the end of the DBA.  Is the DBA going down the road of the dinosaur? I think the answer is more nuanced than that. Firstly lets examine a few trends we are seeing in the marketplace that are going to have a great downstream impact on the technology workplace.

Continue reading

Getting started with user management in MongoDB

One of the first tasks after getting your MongoDB database server up and running is to get your users and database configured. In this blog post we will go over some of the common scenarios  of creating and configuring users in MongoDB. MongoDB user management has improved very significantly over the previous two releases and is now a capable and functional user management model. Users can be assigned various roles and roles have privileges. There are several built in user roles or you can create your own custom roles.

The examples in this post use a 2.6.4 client and a 2.6.4 server. Considerable changes were made to the user management model from 2.4 to 2.6.  So if you are using a 2.4 client a lot of the examples in this blog post are not going to work. You can check the version of your mongodb client using the following syntax

mongo --version

Adding a user to a database

The first step after creating your user is to create your application database

use applicationdb;

Now after creating this database we want to create the user that will be used by the application to write to this database. We want this user to have read & write privileges to the database

db.createUser({'user':'appuser', 'pwd':'', roles:['readWrite']});

Sometimes we also want to add users who have read only access to the db. E.g. we might want to add a analytics user who only has read only access to the db

db.createUser({'user':'analyticsuser', 'pwd':'', roles:['read']});

Now that the users are created lets try and connect as this user from the mongodb console

mongo -u 'appuser' -p  <servername>/applicationdb
MongoDB shell version: 2.6.4
connecting to: <servername>/applicationdb

So we were able to successfully connect. Note the “/applicationdb” at the end of the syntax tells mongodb to authenticate the ‘appuser’ on the ‘applicationdb’ database

Adding a user to multiple databases

In many scenarios we need to create multiple databases on the server. For example in this scenario we might need to create another database ‘analyticsdb’ to store the results of the analytics. The ‘analyticsuser’ now needs ‘readonly’ access on the ‘applicationdb’ and ‘readWrite’ permissions on the ‘analyticsdb’.

So how do we achieve this? Should we add the ‘analyticsuser’ to each database? This creates a management nightmare over the long term as many users and databases are added. Fortunately there is a simple solution. We can centralize the role assignments for a user and store them in a single database. In this scenario I prefer to store these assignments in the ‘admin’ db since it is the hub of central administration in the server, but you can also store it in a separate db.

use admin
db.createUser({user:'analyticsuser', pwd:'<pass>', roles:[{'role':'read', 'db':'applicationdb'}, { 'role':'readWrite', 'db':'analyticsdb'}]});

Once it is added you can use ‘show users’ to show the details of your users. Here is what my admin db looks like

use admin
> show users
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"roles" : [{ "role" : "root","db" : "admin"},{"role" : "restore","db" : "admin"}]
{"_id" : "admin.analyticsuser",
"user" : "analyticsuser",
"db" : "admin",
"roles" : [{"role" : "read","db" : "applicationdb"},{"role" : "readWrite","db" : 'analyticsdb"}]

Continue reading

MongoDB Seattle 2014

Hope to see everybody at MongoDB Seattle, an annual one-day conference for developers, architects and operations professionals to deepen their knowledge and expertise of MongoDB.

MongoDB Seattle will take place on September 16th at the Bell Harbor Conference Centre. This highly productive day of learning and fun will feature advanced technical talks, partner sessions, and one-on-one time with MongoDB experts.

Come stop by our booth and register to win a free Amazon kindle fire that we are giving away!

Continue reading

MongoDB analytics series: Slamdata – Run SQL and build reports directly on MongoDB

This a guest post by John A. De Goes . John is the CTO & co-founder of SlamData. When not working on tricky compilation issues for SlamData, you can find John speaking at conferences, blogging, spending time with his family, and being active in the foothills of the Rocky Mountains. Contact John at

MongoDB has been hugely successful in the developer community, partially because it allows developers to store data structures directly in a fast, scalable, modern database.

There's no need to map those data structures to rigid, predefined, and flat tables that have to be reassembled at runtime through lots of intermediate tables. (Described that way, the relational model sounds kind of old fashioned, doesn't it?)

Unfortunately, the world's analytics and reporting software can't make sense of post-relational data. If it isn't flat, if it isn't all uniform, you can't do anything with it inside legacy analytics and reporting solutions!

Continue reading

Understanding durability & write safety in MongoDB

Durability is the “D” in the “ACID” properties popularized by traditional RDBMS. Durability is the guarantee that written data has been saved and will survive permanently. NoSQL databases like MongoDB give developers fine grained control over the durability of their write calls. This enables developers to choose different durability, safety and performance models for different classes of data. However this also places the burden on the developer to discern and understand the nuances of the different write safety options. In this blog post we will look at the different options for write safety provided in the Java driver. In MongoDB parlance this is called “Write Concern”. Write concerns vary from “weak” to “strong”. Weak writes concerns can lead to higher throughput but provide less data safety and strong write concerns are vice versa.

Continue reading

How to find a needle in a haystack?

 Needle In A Haystack Loupe DrawingThe poster child scenario for big data – you need to sift through a large amount of data to extract a tiny “nugget” of information. Also you need to do it in as short a amount of time as possible, your business depends on it. Historically using traditional RDBMS technology this sort of scenario has required a large team and a large investment of time and money. Most traditional RDBMS’s only scale vertically, so you have to keep buying larger and larger machines to reduce your turnaround time. The advent of public clouds and NoSQL databases like MongoDB has completely disrupted how teams are thinking about this scenario.

Continue reading

Implementing pagination with MongoDB, Express.js & Slush

MongoDB accepts and provides access to data in the Javascript Object notation (JSON) format. This makes MongoDB a perfect fit when dealing with javascript based REST services. In this post, we will take a look at Pagination using MongoDB. We will scaffold a simple Express/Mongojs application using slush-mongo. Then we will use skip() and limit() to fetch the required records from a set of data.

Pagination is one of the simplest ways to increase UX when dealing with average to huge data sets. We split the entire data into x records per page and the we will have (total records/x) pages and then we show a pagination with the number of page. As the user clicks on the page number, we seek and fetch the set of records for that particular view only.


You can find a live demo of the app here and the complete code for this app here.

Setup the Project

Create a new folder named mongoDBPagination. Open terminal/prompt here. Next, we will install gulp, slush and slush-mongo modules. Run

$ [sudo] npm i -g gulp slush slush-mongo

Once this is done, run

$ slush mongo

You will be asked a few questions and you can answer it as follows

[?] Which MongoDB project would you like to generate? Mongojs/Express
[?] What is the name of your app? mongoDBPagination
[?] Database Name: myDb
[?] Database Host: localhost
[?] Database User:
[?] Database Password:
[?] Database Port: 27017
[?] Will you be using heroku? (Y/n) n

This will scaffold a simple Express/Mongojs app for us. Once the installation is done, run

$ gulp

Then open http://localhost:3000 in your favorite browser and you should see a table with list of routes configured in the application. This confirms that you have installed everything correctly.

Setup Test DB

We will create a new collection named ‘testData‘ and then populate some test data in it. Then we will show this data in a paginated table. Open a new Terminal/prompt and run

$ mongo

Then run

use myDb

to select our DB. Next copy the snippet below and paste it in the mongo shell and hit return.

for(var i = 1; i <= 999; i++) {

 name: Math.random()

 age: Math.floor(Math.random() * 99),

 random: Math.random()

Continue reading

The three A’s of MongoDB security – Authentication, Authorization & Auditing

The three A's of Mongodb security - Authentication, Authorization and Auditing

MongoDB, Inc has made impressive strides over the past 18 months. One of the areas of the product that has seen the most significant improvement has been the area of Security. Security is of paramout importance for a production database. Existing relational databases provides a  number of knobs and controls to help the DB administrator manage the security of his database and MongoDB is getting to a similar place as well. In this post we will delve deeper into the security features in the areas of Authentication, Authorization & Auditing.

Continue reading

MongoDB shards and unbalanced aggregation loads

The aggregation framework is a vital cog in the mongodb infrastructure. It helps you analyze, summarize and aggregate the data stored in mongodb. Refer to this blog post for more details about the aggregation framework in MongoDB 2.6.

In the 2.6 release MongoDB made a subtle but significant change in the way the underlying aggregation pipelines execute in a sharded environment. When working with sharded collections MongoDB splits the pipeline into two stages. The first stage or the “$match” phase runs on each shard and selects the relevant documents. If the query planner determines that a shard is not relevant based on the shard keys then this phase is not executed on that shard.

Continue reading

Yeoman, Mongoose and MongoDB

In our previous post we talked about getting started with Mongoose and MongoDB. In this post, we will see how to use Yeoman and scaffold a new Mongoose/Express project.

Yeoman is a scaffolding tool, that scaffolds out projects using Grunt, Bower and Node. There are times when you end up cut ‘n pasting boilerplate code around to create a new project. This is precisely what Yeoman does, but with a single command and a few awesome generators.

Yeoman uses Grunt as the taskrunner to perform run/build/test tasks. If you want to use Gulp for the same, you can checkout Slush. Slush is also a Scaffolding tool but uses Gulp as the taskrunner.

Getting Started with Yeoman

To make our lives easy, we will be using a Super Awesome Yeoman Generator named generator-mongoose, which will help us in setting up a new project as well as help us in scaffolding schemas.

This generator uses Express js as the server, HTML for templating and a tinge of Bootstrap CSS to make things look good.

Let’s create a new folder and name it yoMongoose. CD into the folder and run the following :
To install Yeoman

[sudo] npm install -g yo

To install generator-mongoose

[sudo] npm install -g generator-mongoose

and finally run

yo mongoose

to scaffold a new project. Fill in the question like

[?] Database Name: (myDb) myTestDB
[?] Database Host: (localhost) localhost
[?] Database User: {hit return}
[?] Database Password: {hit return}
[?] Database Port: (27017) 27017
[?] Will you be using heroku? (Y/n)  n

And yeoman will go off and scaffold a new project for you. Your folder structure should consist of a /node_modules folder and a public/bower_components. If you do not see either of them, please run npm install and bower install.

To run the app, execute


This will start off the express server and launch the home page in your default browser. The default page you see is a list of routes configured in the application.

Back to the folder and let’s have a quick walkthrough of the app.

config/db.js – consist of the DB configs and some options you can mess around with

models/post.js – is an example schema of a blog post. All the other models, which we are going to scaffold with the sub generator will appear here.

public/ – consist of the Javascript and CSS needed for the UI

index.js – consist of the default route, that will dispatch the index.html
post.js – consist of 5 key endpoints you need to interact with the posts collection

test/ – consists of the test for Post route and its methods

views/ – consists of all the templates & views sent to the client.

I recommend taking a peek at the following in order


to get a feel of where things go in a modular Express app. Once you are done, we will scaffold another model named articles using the sub generator.

Back to terminal/prompt and run

yo mongoose:schema "article|title:String,excerpt:String,content:String,published:Boolean,created:Date"

the above command will result in

Your creating a schema for article
With the fields: title,excerpt,content,published,created
starting request to schematic for test mock data...
create routes/article.js
create models/article.js
create test/test-article.js

Continue reading