A simple Dart program with MongoDb on Ubuntu

What is Dart? – Dart is a new language, with tools and libraries for structured web app engineering according to its’ official web site http://www.dartlang.org/ . The most important features I have found in dart is its simplicity (single language to both server side and client side, avoiding complex features ), faster performance, easier to setup build environment etc.

What is MongoDb? – MongoDb is a NoSQL database where you can store data as documents which are scalable and flexible. http://www.mongodb.org/about/introduction/

Setting up the environment

Installing dart is an easier task. The installation package can be downloaded from http://www.dartlang.org/tools/editor/  and the user only has to unzip it. No other configuration is needed, but if someone need to get configuration details those are also available in the above link. By simply clicking on the DartEditor in the dart folder created by the extractions of the above, you can start coding Dart.

Installing MongoDb is easier than that. By typing  sudo apt-get install mongodb on terminal MongoDb will be installed. After the installation Mongo services will run automatically. If it is not type sudo service mongodb start on terminal. Now the database service is available. By typing mongo on terminal, you can get the MongoDb shell. Using Mongo shell all the database features could be handled. For more info browse http://docs.mongodb.org/manual/contents/ .

Start Creating a Database

I create the same database as mentioned in http://docs.mongodb.org/manual/core/create/ as the database example here. The database name is test. Collection name is bios.

>db.bios.insert( {_id: 1,name: { first: 'John', last: 'Backus' }, birth: new Date('Dec 03, 1924'), death: new Date('Mar 17, 2007') } )

To check whether the data we entered are there, it can be checked with following.

> db.bios.find();
{ "_id" : 1, "name" : { "first" : "John", "last" : "Backus" }, "birth" : ISODate("1924-12-02T18:30:00Z"), "death" : ISODate("2007-03-16T18:30:00Z") }

Start Creating the Application

Now go to the Dart Editor and create new Application. Select command-line application and then click on finish  I named it here as MongoTest.

Screenshot from 2013-07-09 22:53:13

Now the package mongo_dart should be added. For that open pubspec.yaml file in Folder MongoTest->packages in side bar. You will find a section as dependencies. Click on Add button and then type mongo_dart in the dialog appears. Then click Ok button and save by pressing Ctrl+S.  For further details on package manager in Dart, go to http://pub.dartlang.org .

Now open mongotest.dart, You can import the mongo_dart library by adding the following line.

import 'package:mongo_dart/mongo_dart.dart';

Now you will be able to call all the functions which are mentioned in mongo_dart API reference. http://pub.dartlang.org/packages/mongo_dart .

Following code will enable you to connect with MongoDb and fetch data. In the Dart Editor Console the output will be displayed.

import 'package:mongo_dart/mongo_dart.dart';

void main() {

void dbConnection() {
var db = new Db('mongodb://');
var _bios = db.collection("bios");
Cursor cur = _bios.find();
cur.each((Map bio){

After that press the launch button in the editor. If the launch was not successful and having errors with Dartium try the instructions in the following link   http://code.google.com/p/dart/wiki/PreparingYourMachine#Linux . The instructions given in that didn’t work for me. What I had done was, followed instructions in https://groups.google.com/a/dartlang.org/forum/#!topic/misc/V3u8oogUDxQ and install the Dartium using command line.

sudo add-apt-repository ppa:hachre/dart
sudo apt-get update
sudo apt-get install darteditor dartsdk dartium
Then It worked for me. (Run with Dartium)
Do not import ‘dart:html’ . Then it will ask for associated .html files . The package mongo_dart is using ‘dart:io’ . It works only in the server side.
The following result will be appeared in the DartEditor console.
{_id: 1.0, name: {first: John, last: Backus}, birth: 1924-12-03 00:00:00.000, death: 2007-03-17 00:00:00.000}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s