Go Couch supports searches

Until version 2.0 the CouchDB used views and here map/reduce to provide access to groups of documents. Today there’s also the find request using Mango Queries to find the decuments of interest.

With the latest beta update the Tideland Go CouchDB Client contains the find package to create these queries the Go way and perform flexible parametrized requests to find the documents you want.

Imagine a database containing documents with documents describing persons with their age and their activity state. You want to find the special ones - the young inactive or the old active. The variable cdb below references the database the typical client way.

selector := find.Select(
            find.LowerThan("age", 30),
            find.Equal("active", false),
            find.GreaterThan("age", 60),
            find.Equal("active", "true"),
frs := find.Find(cdb, selector, find.Fields("name", "age", "active"))

if !frs.IsOK() {
    return frs.Error()

err := frs.Do(func(document couchdb.Unmarshable) error {
    fields := struct {
        Name   string `json:"name"`
        Age    int    `json:"age"`
        Active bool   `json:"active"`
    if err := document.Unmarshal(&fields); err != nil {
        return err
    fmt.Printf("name: %s age: %d activity status: %v \n",
        fields.Name, fields.Age, fields.Active)
    return nil

Results also can be sorted. But here constraints and restrictions by CouchDB exist. So I’ll add an extra post about it soon.

More about the project at PROJECTS → Go CouchDB Client.

comments powered by Disqus