A proof of concept MongoDB clone built on Postgres
Clone or download
JerrySievert Merge pull request #13 from deafbybeheading/patch-1
Fix slightly-less-easy install instructions
Latest commit 0177544 Aug 23, 2014
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin fix argument list Apr 3, 2014
data add more sample data Feb 4, 2014
sql more todo Feb 4, 2014
test fix argument list Apr 3, 2014
.gitignore working tests! Nov 29, 2013
.npmignore working tests! Nov 29, 2013
LICENSE Create LICENSE Jan 2, 2013
README.md Fix slightly-less-easy install instructions Mar 25, 2014
package.json update version Feb 4, 2014



Mongolike is an experimental MongoDB clone being built on top of PLV8 and Postgres.

Implemented (so far)

  • create_collection()
  • drop_collection()
  • save()
  • find()
  • runCommand() (Map/Reduce)
  • ensureIndex()
  • removeIndex()
  • getIndexes()


Install PLV8

Visit http://code.google.com/p/plv8js/wiki/PLV8 and follow the build instructions.

Install Mongolike

The Easy Way

The easy way to install is to use node.js.

$ npm install -g mongolike
$ mongolike-install -d yourdb

The Slightly Less Easy Way

$ for file in sql/*.sql; do psql yourdb < $file; done

Running Tests

Mongolike includes a test suite and a test runner.

$ test/test_runner.js -d yourdb

Additional tests can be added to test/tests.sql.


All commands must be prefixed by SELECT, and are modified slightly to work in the Postgres environment.


Create a collection.


SELECT create_collection('test');


Drop a collection.


SELECT drop_collection('test');

save(collection, object)

Save an object into a collection.


SELECT save('test', '{ "foo": "bar" }');

find(collection /*, terms, limit, skip */)

Find an object, with optional terms, limit, and skip.


SELECT find('test', '{ "type": { "$in": [ "food", "snacks" ] } }');


Run a command on the Database. Currently only mapReduce is supported.

NOTE The JSON object cannot have carriage returns, the example below does for readability.


SELECT runCommand('{
  "map": "function MapCode() {
    emit(this.Country, {
      \"data\": [
          \"city\": this.City, 
          \"lat\":  this.Latitude, 
          \"lon\":  this.Longitude
  "reduce": "function ReduceCode(key, values) {
    var reduced = {
      \"data\": [ ]
    for (var i in values) {
      var inter = values[i];
      for (var j in inter.data) {
    return reduced;
  "mapreduce": "cities",
  "finalize": "function Finalize(key, reduced) {
    if (reduced.data.length == 1) {
      return {
        \"message\" : \"This Country contains only 1 City\"

    var min_dist = 999999999999;
    var city1 = { \"name\": \"\" };
    var city2 = { \"name\": \"\" };
    var c1;
    var c2;
    var d;

    for (var i in reduced.data) {
      for (var j in reduced.data) {
        if (i >= j) continue;
        c1 = reduced.data[i];
        c2 = reduced.data[j];
        d = Math.sqrt((c1.lat-c2.lat)*(c1.lat-c2.lat)+(c1.lon-c2.lon)*(c1.lon-c2.lon));

        if (d < min_dist && d > 0) {
          min_dist = d;
          city1 = c1;
          city2 = c2;
    return {
      \"city1\": city1.city,
      \"city2\": city2.city,
      \"dist\": min_dist
  }" }');

ensureIndex(collection, terms /*, type */)

Creates a new index on a collection.


SELECT ensureIndex('test', '{ "foo", "bar" }', '{ "unique": true }');

removeIndex(collection, name)

Removes an index from a collection by name.


SELECT removeIndex('test', 'idx_col_woo_foo');

removeIndex(collection, terms)

Removes an index from a collection by terms.

NOTE in order to remove an index with terms you MUST cast the query due to how Postgres handles JSON.


SELECT removeIndex('test', '{ "foo", "bar" }'::json);


Retrieves all indexes for a given collection.


SELECT getIndexes('test');

Importing the Data

I have included a modest amount of data for testing and benchmarking, both for Postgres and for MongoDB (1,706,873 rows).

Importing into Postgres:

$ psql yourdb < data/cities.sql

This will create the collection and save() all of the data.

Importing into MongoDB

$ mongoimport --collection cities --type csv --headerline --file data/cities.csv --db yourdb

Follow along at http://legitimatesounding.com/blog/