Go - Collection.Query.Where()

Adds a new where clause to a query, which filters the data returned.

import (
  "fmt"

  "github.com/nitrictech/go-sdk/api/documents"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  profiles, err := nitric.NewCollection("profiles").With(nitric.CollectionReading)
  if err != nil {
    return
  }

  query := profiles.Query().Where(
    documents.Condition("name").StartsWith(documents.StringValue("T")),
  )

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Parameters

  • Name
    expressions
    Required
    Required
    Type
    ...QueryExpression
    Description

    The query expressions.

Notes

Multiple query expressions combined together are always considered AND

Examples

A simple query

import (
  "fmt"

  "github.com/nitrictech/go-sdk/api/documents"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  profiles, err := nitric.NewCollection("profiles").With(nitric.CollectionReading)
  if err != nil {
    return
  }

  query := profiles.Query().Where(
    documents.Condition("firstName").Eq(documents.StringValue("Drake")),
  )

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Combining where clauses

import (
  "fmt"

  "github.com/nitrictech/go-sdk/api/documents"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  profiles, err := nitric.NewCollection("profiles").With(nitric.CollectionReading)
  if err != nil {
    return
  }

  query := profiles.Query().Where(
    documents.Condition("name").Eq(documents.StringValue("Drake")),
    documents.Condition("age").Gt(documents.DoubleValue(21)),
  )

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

See also