- 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.
Let me resend it.