Announcing mgo: a rich MongoDB driver for Go

2,830 views
Skip to first unread message

Gustavo Niemeyer

unread,
Mar 14, 2011, 11:19:53 PM3/14/11
to golang-nuts
Alright, and here it is, after a few months and my Christmas holidays! ;-)

http://labix.org/mgo

As you'll notice, this driver offers features which aren't really
available in *any* of the MongoDB drivers out there. Not even the
official ones provided by 10gen. I'm not writing this to compete with
the existing Go drivers, though, but to enable me to use MongoDB to
solve some fun problems.

So, enjoy, and let me know if you find issues or lacking features.
I'll be happy to fix anything promptly, since I'll be soon depending
on the driver too.

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter

Evan Shaw

unread,
Mar 15, 2011, 12:22:22 AM3/15/11
to golang-nuts
In an effort to reduce the crowded landscape of Go BSON serializers
and MongoDB drivers, I've decided to discontinue my projects, mongogo
and go-bson. (I'm pretty sure no one's using them anyway. :)) I'll be
helping Gustavo out on his new projects where I can.

- Evan

Gustavo Niemeyer

unread,
Mar 15, 2011, 12:26:54 AM3/15/11
to Evan Shaw, golang-nuts

Thank you very much for the merger Evan, and welcome to the development team.

Eric Clark

unread,
Mar 15, 2011, 12:32:50 AM3/15/11
to Gustavo Niemeyer, golang-nuts
On Mon, Mar 14, 2011 at 10:19 PM, Gustavo Niemeyer <gus...@niemeyer.net> wrote:
>
> So, enjoy, and let me know if you find issues or lacking features.
> I'll be happy to fix anything promptly, since I'll be soon depending
> on the driver too.

This is fantastic. I have been wanting a more complete mongo driver.
I recently wrote my own bson serializer since the driver I'm using now
was placing things in a map and losing the field ordering. A cursory
look at your code leads me to believe that it won't be a problem.
I'll switch to using mgo and send you feedback and/or patches.

Thanks!

Johann Höchtl

unread,
Mar 15, 2011, 5:01:40 AM3/15/11
to golang-nuts


On Mar 15, 4:19 am, Gustavo Niemeyer <gust...@niemeyer.net> wrote:
> Alright, and here it is, after a few months and my Christmas holidays! ;-)
>
>    http://labix.org/mgo
>

Drop the people at Mongodb a line to include it as a community
supported driver:
http://www.mongodb.org/display/DOCS/Drivers

Johann
> --
> Gustavo Niemeyerhttp://niemeyer.nethttp://niemeyer.net/bloghttp://niemeyer.net/twitter

Gustavo Niemeyer

unread,
Mar 15, 2011, 8:05:00 AM3/15/11
to gary b, golang-nuts
> Wow, mgo is impressive.  Here are a few things from go-mongo that you
> might find useful for mgo.  Let me know if you are open to receiving
> patches with any of these features.

Thanks a lot Gary. You're most welcome to join the development too.

In certain cases, we'll just have to agree on what is the intended
interface and/or outcome for the feature. I'm CCing golang-nuts
again, just so other people interested on those features can also
follow up and contribute to the discussion.

> == CommandResponse: https://github.com/garyburd/go-mongo/blob/master/helpers.go#L72
>
> The CommandResponse type declares the common fields in command
> responses.  The CommandResponse Error() method examines the fields and
> returns an appropriate os.Error.  Here's an example use in the count

Cases where we're sure such a command would be a good idea seem like a
good chance to detect and return an actual error in the usual os.Error
convention for Go, rather than requiring the user to deal with it by
hand externally.

As an example, check out how Insert() and Update() take care of
unmarshalling a LastError instance when necessary, so that the
traditional Go idiom works fine:

if err := collection.Insert(value); err != nil {
...
}

> == BSONData: https://github.com/garyburd/go-mongo/blob/master/helpers.go#L234
>
> BSONData represents a chunk of uninterpreted BSON data.  Fields of
> this type are copied directly to and from the BSON encoding with no

Check out the gobson package API regarding the Getter and Setter
interfaces. It makes this process much simpler: if you want a custom
serialization, just define the Getter interface for a type, and the
result returned by this method will be serialized in place of the
value itself.

Note that it will *serialize* the returned values, rather than using
the raw data. There's no reason to ask people to deal with binary
mangling in those circumstances. All the types supported by BSON are
contemplated by gobson, so it is possible to obtain any desired
serialization without having to generate the data by hand.

> == Tailable cursors
>
> I didn't see support for tailable cursors, but I might have missed it
> because tailable cursor support boils down to a few if statements here

This is something which is not supported right now, but was already in
the pipeline and will surely be available in the next release since I
need it too.

Your input/experience/patch for this will certainly be welcome. I
would just like to debate a bit about the actual API, before we go
into implementation details. Some of the APIs I've seen in other
drivers from 10gen for dealing with tailable cursors felt cumbersome
to use in practice, so I'd like to get to a more comfortable API if we
can manage to.

How do you feel we could best support this?

Gustavo Niemeyer

unread,
Mar 15, 2011, 8:09:20 AM3/15/11
to Eric Clark, golang-nuts
> This is fantastic.  I have been wanting a more complete mongo driver.
> I recently wrote my own bson serializer since the driver I'm using now
> was placing things in a map and losing the field ordering.  A cursory
> look at your code leads me to believe that it won't be a problem.
> I'll switch to using mgo and send you feedback and/or patches.

Right, when the order is important, you can either use a struct, or if
a map is more comfortable the gobson.D may also be useful:

http://goneat.org/pkg/launchpad.net/gobson/#D

Please let me know if you need anything else.

Fabio Kaminski

unread,
Mar 15, 2011, 9:07:13 AM3/15/11
to Gustavo Niemeyer, Eric Clark, golang-nuts
Very well written, congrats!! lucky me that i didnt not launch my "baby" yet, so i can adapt to this new driver.. 

Gustavo Niemeyer

unread,
Mar 15, 2011, 10:45:21 AM3/15/11
to gary b, golang-nuts
> In certain cases, we'll just have to agree on what is the intended
> interface and/or outcome for the feature.  I'm CCing golang-nuts
> again, just so other people interested on those features can also

As suggested by Gary, we've created a mailing list to debate about
usage and upcoming features of mgo:

http://groups.google.com/group/mgo-users

Please join if you're interested in following up.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages