MooDB the embedded NoSQL Database.

Announcing MooDB: an embedded NoSQL Database. MooDB is built on top of Sqlite and provides a simple C/C++ API. MooDB stores data in JSON objects as key/value pairs. There is a built-in map-reduce framework to make querying data fast. Map-reduce functions are written in javascript so any web developer will be familiar. A lot of the concepts from MooDB are borrowed from CouchDB.

Find the MooDB project hosted at:

Open/Close Database connection

moodb* db; moodbopen("test1.db", &db); .... moodbclose(db);

Insert/Retrive/Delete Documents
Documents are stored in key/value pairs. Keys can be a string of any length. Documents are stored as JSON strings. Documents can be as complex or simple as you like. Documents do not need to conform to any type of schema.

moodbputobject(db, "myobjectkey", "({ firstname: \"John\", lastname: \"Doe\", age:50, eyecolor: \"blue\" })", NULL); char *jsondata; moodbgetobject(db, "myobjectkey", &jsondata); moodbfree(jsondata); moodbdeleteobject(db, "myobjectkey");

Views are the heart of MooDB’s map-reduce system. Views are used to aggregate and display data stored in MooDB. Views are created dynamically and do not affect the underlying document data. You can have as many different view representations of the same data as you like. Each view creates an index on the key/value pairs emitted from the map function. This makes querying views very fast.

Let’s say you want to be able to quickly search on the ‘eyecolor’ attribute of all the documents in your database.

moodb_putview(db, "({name: \"eyecolor\", map: function(doc) {" "if(doc.eyecolor) {emit(\"eyecolor\", doc.eyecolor);} " "} })");

This example creates a view named “eyecolor.” Views always need to have at minimum a unique name and a map function. Map functions always take a single argument: doc. The map function is called once for each document in the database. The emit function always take two arguments: emit(key, value). Each time emit is called, it creates a view result. View results are indexed and stored ready to be used for queries later.

Views are incrementally updated every time a document is inserted, modified, or deleted.

Now to query all of the objects that have ‘green’ eyes:

moocursor *cursor; moodbquery(db, &cursor, "({view: \"meyecolor\", filter: \"eyecolor = green\" })"); char *key, *value; while(moodbcursornext(cursor, &key, &value) == MOODBOK){ printf("%s : %s\n", key, value); } moodbcursor_close(cursor);

Paul Soucy

Read more posts by this author.