-
Notifications
You must be signed in to change notification settings - Fork 7
Filtering
Jason Chu edited this page Jun 17, 2017
·
3 revisions
Filtering operates on Range
s. It scans the range and skips any documents that the provided filter function returns false for.
func (r *Range) Filter(filter func(doc Document) (bool, error)) *Range
package main
import (
"github.com/1lann/cete"
"fmt"
)
type Person struct {
Name string
Age int
}
func main() {
db, _ := cete.Open("./cete_data")
defer db.Close()
db.NewTable("people")
db.Table("people").Set("ash", Person{
Name: "Ash Ketchum",
Age: 10,
})
db.Table("people").Set("brock", Person{
Name: "Brock",
Age: 15,
})
// Find who is older than 13
r := db.Table("people").All().Filter(func(doc cete.Document) (bool, error) {
return doc.QueryInt("Age") > 13, nil
})
var person Person
r.Next(&person)
fmt.Printf("%+v\n", person) // Should print Brock's information
}
// Find whose name starts with Ash
r := db.Table("people").All().Filter(func(doc cete.Document) (bool, error) {
return strings.HasPrefix(doc.QueryString("Name"), "Ash"), nil
})
// You can reference other tables in your filter and specify the number
// of workers to use when filtering to speed up the filter.
r := db.Table("people").All().Filter(func(doc cete.Document) (bool, error) {
var poke Pokemon
_, _, err := db.Table("pokemon").Index("trainer").One(doc.QueryString("Name"), &poke)
return poke.Name == "Pikachu", err
}, 20) // <-- Note that the number of workers is specified here