Go - Collection.Doc.Set()

Set the value of a document.

import (
  "context"
  "fmt"

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

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

  drakeProfileRef := profiles.Doc("Drake Mallard")

  err = drakeProfileRef.Set(context.TODO(), map[string]interface{}{
    "firstName": "Drake",
    "lastName": "Mallard",
  })
  if err != nil {
    return
  }

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

Parameters

  • Name
    ctx
    Required
    Required
    Type
    context
    Description

    The context of the call, used for tracing.

  • Name
    document
    Required
    Required
    Type
    map[string]interface{}
    Description

    The document to set on the key

Examples

Set a document

import (
  "context"
  "fmt"

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

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

  drakeProfileRef := profiles.Doc("Drake Mallard")

  err = drakeProfileRef.Set(context.TODO(), map[string]interface{}{
    "firstName": "Drake",
    "lastName": "Mallard",
  })
  if err != nil {
    return
  }

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

Update a document

import (
  "context"
  "fmt"

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

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

  drakeProfileRef := profiles.Doc("Drake Mallard")

  existingProfile, err := drakeProfileRef.Get(context.TODO())
  if err != nil {
    return
  }

  contents := existingProfile.Content()
  contents["firstName"] = "Drake"

  err = drakeProfileRef.Set(context.TODO(), contents)
  if err != nil {
    return
  }

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

See also