[ANN] mgo r2011.10.29 with simplified iteration

8 views
Skip to first unread message

Gustavo Niemeyer

unread,
Oct 29, 2011, 12:48:24 PM10/29/11
to golan...@googlegroups.com, mongod...@googlegroups.com, mgo-...@googlegroups.com
Greetings,
After a few weeks without new releases, here is a freshnew release of
the mgo MongoDB driver for Go with someuseful improvements.
The project page with details is available at:
    http://labix.org/mgo
The focus of release r2011.10.29 is a much needed simplificationon the
way result sets are iterated over. Please read throughsince this is
not a backwards compatible change. I apologizefor the trouble in
advance, but I'm sure you'll appreciate it.
The changes made are:

- Simplified the Iter interface significantly. Previously, an
iteration loop would look something like this:
      iter, err := query.Iter()      if err != nil {
panic(err)      }      for {          err := iter.Next(&result)  if
err == mgo.NotFound {      break  }  if err != nil {
panic(err)  }  ...      }
  Now, the same loop is written as:
      iter := query.Iter()      for iter.Next(&result) {          ...
    }      if iter.Err() != nil {          panic(iter.Err())      }
  See more details in the documentation:
      http://goneat.org/lp/mgo#Iter.Next
      http://goneat.org/lp/mgo#Iter.Err      http://goneat.org/lp/mgo#Query.Iter


- Tailable cursors were also changed accordingly, but were also
simplified further through the introduction of the Iter.Timeout
method that reports whether a false Next is a timeout or not.
  See the documentation for details and an example:
      http://goneat.org/lp/mgo#Query.Tail
http://goneat.org/lp/mgo#Iter.Timeout

- New convenience Query.All and Iter.All methods, enabling one to
trivially retrieve the whole result set at once into a slice.
  For example:
      var result []struct{ Value int }      err :=
collection.Find(nil).Limit(100).All(&result)
  Please note that the result set must obviously have a bound  size
for All to be used like this, or it will potentially  crash the system
out of memory. Even if you think you know  the set size, consider
limiting the size as done in the example  above just in case, or use
the simplified iteration method  described above.
  More details in the documentation:
      http://goneat.org/lp/mgo#Iter.All
      http://goneat.org/lp/mgo#Query.All


- Given the above enhancements, the Iter.For and Query.For methods
are both considered obsolete now, and while they haven't been  touched
to give people some time to migrate away, they will  be removed in a
future release.

- Update and UpdateAll were fixed to handle a nil selector  parameter
in an equivalent way to an empty map.

Enjoy!

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

-- I never filed a patent.

Gustavo Niemeyer

unread,
Oct 29, 2011, 12:57:53 PM10/29/11
to golan...@googlegroups.com, mongod...@googlegroups.com, mgo-...@googlegroups.com
Ouch.. and it happened again.. for some reason copy & paste and gmail
don't get along very well.

Let me resend it.

Reply all
Reply to author
Forward
0 new messages