Skip to content
This repository

A Mongo Driver for GLib

branch: master
Octocat-spinner-32 cut-n-paste [guri] Add GUri from glib branch for parsing mongodb:// style URIs. June 28, 2012
Octocat-spinner-32 doc Set title for gtk-doc generated documentation. November 01, 2012
Octocat-spinner-32 examples server.py: add replSetGetStatus(). November 02, 2012
Octocat-spinner-32 m4 build: add gtk-doc.m4 for systems without it. November 13, 2012
Octocat-spinner-32 mongo-glib protocol: use mongo_protocol_next_request_id() in getlasterror command. January 21, 2013
Octocat-spinner-32 tests protocol: use MongoInputStream for reading messages in protocol. December 13, 2012
Octocat-spinner-32 .gitignore client: dont hold reference to client in input stream read loop. December 13, 2012
Octocat-spinner-32 AUTHORS Initial commit. October 22, 2011
Octocat-spinner-32 COPYING Initial commit. October 22, 2011
Octocat-spinner-32 ChangeLog Initial commit. October 22, 2011
Octocat-spinner-32 Makefile.am [build] Use proper doc directory for COPYING and others. November 07, 2012
Octocat-spinner-32 Makefile.introspection Initial commit. October 22, 2011
Octocat-spinner-32 Makefile.tests Initial commit. October 22, 2011
Octocat-spinner-32 NEWS [build] Bump for 0.2.0. June 29, 2012
Octocat-spinner-32 README Fix automake builds. October 31, 2012
Octocat-spinner-32 README.md README: update dependencies. October 31, 2012
Octocat-spinner-32 TODO.md TODO: add some MongoClient TODOs. December 13, 2012
Octocat-spinner-32 autogen.sh Some autotools updating of the build system. October 31, 2012
Octocat-spinner-32 configure.ac build: make tracing and debugging separate options. November 09, 2012
Octocat-spinner-32 mongo-glib-1.0.pc.in [build] Remove data/ subdirectory by placing pkg-config file in root … May 14, 2012
Octocat-spinner-32 mongo-glib.spec.in [rpm] update path to tarball. November 07, 2012
README.md

Mongo-GLib

Introduction

Mongo-GLib is a library written in C to communicate with the MongoDB database in an asynchronous fashion. Even though the library is written in C, it can be used from higher level libraries such as Python and JavaScript through the use of GObject Introspection.

The library supports both being a client and a server of the MongoDB wire protocol. The MongoConnection structure works a lot like the connection objects used in other MongoDB drivers, except asynchronously. You provide a callback for most operations that will be executed upon completion of the operation. Using MongoServer, you can implement the server side of the MongoDB protocol allowing you to do interesting things like storing data in systems other than MongoDB such as Redis.

What it supports

  • BSON
    • Support for most BSON types.
    • Iteration of BSON documents including recursion without allocations.
  • Connection
    • Client support for MongoDB protocol.
    • Don't worry about connecting, it happens automatically and asynchronously without any intervention.
    • Support to auto-reconnect to a new master.
    • Support for execution commmands.
    • Automatically performs a getlasterr command to check status on operations that modify the database.
  • Server
    • Implement your own MongoDB server.
    • Handle requests as they come in, query external systems and provide a reply asynchronously.

What it doesn't support

  • GridFS (patches welcome).
  • Sort support on queries.
  • Index creation.
  • Probably more ...

Dependencies

  • glib2-devel
  • libunistring

Optional

  • gnome-common (if building from git)
  • gtk-doc (for api documentation)

Examples

Implementing a Server in JavaScript

// Requires mongo-glib from master and gobject-introspection. Run with gjs.

let Mongo = imports.gi.Mongo;
let MainLoop = imports.mainloop;
let Server = new Mongo.Server();

// Listen on 27017.
Server.add_inet_port(27017, null, null);

// Handle an incoming OP_QUERY
Server.connect('request-query', function(server, client, message) {
    let bson = Mongo.Bson.new_empty();
    bson.append_string("hello", "world");
    message.set_reply_bson(Mongo.ReplyFlags.NONE, bson);
    return true;
});

// Run the main loop.
MainLoop.run("");

Implementing a Server in Python

from gi.repository import Mongo
from gi.repository import GLib

def handleRequest(server, client, message):
    bson = Mongo.Bson()
    bson.append_string("hello", "world")
    message.set_reply_bson(Mongo.ReplyFlags.NONE, bson)
    return True

Server = Mongo.Server()
Server.add_inet_port(27017, None)
Server.connect('request-query', handleRequest)

GLib.MainLoop().run()
Something went wrong with that request. Please try again.