MongoDB, by the company with the same name (http://www.mongodb.org/), is the most popular database among the NoSQL databases. Let's look at some facts about MongoDB:
mongo_dart
by Vadim Tsushko, Ted Sander, and Paul Evans. This recipe will show you how to create, read, update, and delete actions in a MongoDB database from a Dart app. You can see it in action in the using_mongodb
project.Install the latest production release for your system from http://www.mongodb.org/downloads. This is easy. However, if you need more details, refer to http://docs.mongodb.org/manual/installation/. Start the mongod
server process (for example, from c:mongodbin
on Windows) before the Dart app. To make the jobsdb
database, start a mongo
shell and type use jobsdb
; it's that simple. Alternatively, this can also be done via mongo_dart
. NoSQL databases are schemaless, so the collections (which is what tables are called here) are created when the first document (or record) is inserted.
The application starts from using_mongodb.dart
, where a JobStore
object is created, the database is opened, records are written to the jobs
table, and then these records are retrieved. The code from using_mongodb.dart
is identical to the code from using_postgresql.dart
, except that we now import jobstore_mongodb.dart
; so please refer to the Storing data in PostgreSQL recipe. When running this script, first comment out js.openAndRead();
. In the second run, uncomment this and comment out js.openAndStore();
; otherwise, the reads take place before the inserts are completed. The jobstore_mongodb.dart
code contains the code to talk to the database driver:
import 'package:mongo_dart/mongo_dart.dart'; import 'job.dart'; const String DEFAULT_URI = 'mongodb://127.0.0.1/'; const String DB_NAME = 'jobsdb'; const String COLLECTION_NAME = 'jobs'; class JobStore { final List<Job> jobs = new List(); Job job; Db db; DbCollection jobsColl; JobStore() { // 1- make a new database db = new Db('${DEFAULT_URI}${DB_NAME}'), // make a new collection jobsColl = db.collection(COLLECTION_NAME); } openAndStore() { db.open().then((_) { storeData(); }).catchError(print); } storeData() { for (job in jobs) { insert(job); } } // 2- inserting a document in a collection: insert(Job job) { var jobMap = job.toMap(); jobsColl.insert(jobMap).then((_) { print('inserted job: ${job.type}'), }).catchError(print); } openAndRead() { db.open().then((_) { readData(); }).catchError(print); } // 3- reading documents: readData() { jobsColl.find().toList().then((jobList) { processJob(jobList); }).catchError(print); } // 4- working with document data: processJob(jobList) { jobList.forEach((jobMap) { Job job = new Job.fromMap(jobMap); print('${job.dbKey} - ${job.type} - ${job.salary} - ' '${job.company} - ${job.posted} ${job.open}'), }); } // 5- updating a document: update(Job job) { var jobMap = job.toMap(); jobsColl.update({"dbKey": jobMap["dbKey"]},jobMap).then((_) { print('job updated'), }).catchError(print); } // 6- deleting a document: delete(Job job) { var jobMap = job.toMap(); jobsColl.remove(jobMap).then((_) { print('job updated'), }).catchError(print); } }
After running the preceding script, verify in a mongo
shell with use jobsdb
and db.jobs.find()
that the documents have been inserted, as shown in the following screenshot:
The mongo_dart
library makes it very easy to work with MongoDB. In comment 1, we created the Db
and DbCollection
objects in the constructor of JobStore
. To manipulate documents, you have to first call the open
method on the Db
object.
To write a document to the collection, use the insert
method on the DbCollection
object, but its argument must be in the Map
format. Reading documents is done with the find
method. The list that is returned contains items of type Map
, so to construct real objects, we have to make a new named constructor fromMap
in class Job
. Updating a document uses a method of the same name, but its first argument must be a selector to find the document that must be updated. Deleting a document uses the remove
method.