MongoDB 3.0 (previously known as 2.8) is right around the block, and it’s time to release a few fixes and improvements on the mgo driver for Go to ensure it works fine on that new major server version. Compatibility is being preserved both with old applications and with old servers, so updating should be a smooth experience.
Release r2015.01.24 of mgo includes the following changes:
Support ReplicaSetName in DialInfo
DialInfo now offers a ReplicaSetName field that may contain the name of the MongoDB replica set being connected to. If set, the cluster synchronization routines will prevent communication with any server that does not report itself as part of that replica set.
Feature implemented by Wisdom Omuya.
MongoDB 3.0 support for collection and index listing
MongoDB 3.0 requires the use of commands for listing collections and indexes, and may report long results via cursors that must be iterated over. The CollectionNames and Indexes methods were adapted to support both the old and the new cases.
Introduced Collection.NewIter method
In the last few releases of MongoDB, a growing number of low-level database commands are returning results that include an initial set of documents and one or more cursor ids that should be iterated over for obtaining the remaining documents. Such results defeated one of the goals in mgo’s design: developers should be able to walk around the convenient pre-defined static interfaces when they must, so they don’t have to patch the driver when a feature is not yet covered by the convenience layer.
The introduced NewIter method solves that problem by enabling developers to create normal iterators by providing the initial batch of documents and optionally the cursor id for obtaining the remaining documents, if any.
Thanks to John Morales, Daniel Gottlieb, and Jeff Yemin, from MongoDB Inc, for their help polishing the feature.
Improved JSON unmarshaling of ObjectId
bson.ObjectId can now be unmarshaled correctly from an empty or null JSON string, when it is used as a field in a struct submitted for unmarshaling by the json package.
Improvement suggested by Jason Raede.
Remove GridFS chunks if file insertion fails
When writing a GridFS file, the chunks that hold the file content are written into the database before the document representing the file itself is inserted. This ensures the file is made visible to concurrent readers atomically, when it’s ready to be used by the application. If writing a chunk fails, the call to the file’s Close method will do a best effort to clean up previously written chunks. This logic was improved so that calling Close will also attempt to remove chunks if inserting the file document itself failed.
Improvement suggested by Ed Pelc.
Field weight support for text indexing
The new Index.Weights field allows providing a map of field name to field weight for fine tuning text index creation, as described in the MongoDB documentation.
Feature requested by Egon Elbre.
Fixed support for
$** text index field name
Support for the special
$** field name, which enables the indexing of all document fields, was fixed.
Problem reported by Egon Elbre.
Consider only exported fields on omitempty of structs
The implementation of bson’s omitempty feature was also considering the value of non-exported fields. This was fixed so that only exported fields are taken into account, which is both in line with the overall behavior of the package, and also prevents crashes in cases where the field value cannot be evaluated.
Fix potential deadlock on Iter.Close
It was possible for Iter.Close to deadlock when the associated server was concurrently detected unavailable.
Problem investigated and reported by John Morales.
Return ErrCursor on server cursor timeouts
Attempting to iterate over a cursor that has timed out at the server side will now return mgo.ErrCursor.
Feature implemented by Daniel Gottlieb.
Support for collection repairing
The new Collection.Repair method returns an iterator that goes over all recovered documents in the collection, in a best-effort manner. This is most useful when there are damaged data files. Multiple copies of the same document may be returned by the iterator.
Feature contributed by Mike O’Brien.