Skip to content

Instantly share code, notes, and snippets.

@coryschwartz
Created December 15, 2023 22:34
Show Gist options
  • Select an option

  • Save coryschwartz/80c37f15f58ddf0d8c979073f30d6ccf to your computer and use it in GitHub Desktop.

Select an option

Save coryschwartz/80c37f15f58ddf0d8c979073f30d6ccf to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"io"
"log"
"gocloud.dev/docstore"
_ "gocloud.dev/docstore/gcpfirestore"
_ "gocloud.dev/docstore/memdocstore"
)
type doc struct {
A string
B int
}
func main() {
ctx := context.Background()
coll, err := docstore.OpenCollection(context.Background(), "firestore://projects/pinion-testing/databases/(default)/documents/testtesttest?name_field=A")
// coll, err := docstore.OpenCollection(context.Background(), "mem://collection/A")
if err != nil {
log.Fatal(err)
}
defer coll.Close()
actions := coll.Actions()
actions.Put(&doc{A: "W", B: 1})
actions.Put(&doc{A: "X", B: 1})
actions.Put(&doc{A: "Y", B: 2})
actions.Put(&doc{A: "Z", B: 3})
if err := actions.Do(ctx); err != nil {
log.Fatal(err)
}
qrys := []string{"in", "not-in"}
interestingB := []int{1, 2}
for _, qry := range qrys {
fmt.Println(qry)
iter := coll.Query().Where("B", qry, interestingB).Get(ctx)
defer iter.Stop()
for {
d := new(doc)
err := iter.Next(ctx, d)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(d.A)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment