-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_document.go
70 lines (59 loc) · 1.45 KB
/
add_document.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
//"cloud.google.com/go/firestore"
//"encoding/json"
"fmt"
"google.golang.org/api/iterator"
"log"
"time"
)
func addDocument(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
if collection == "" {
fmt.Printf("no collection set - use %q\n", "setcollection")
return
}
//document := `{
// "name": `+string(firestore.ServerTimestamp)+`,
// "country": 123,
// "city": "Wuhan",
// "reason": "Non vedge Food"
//}`
type State struct {
Capital string `firestore:"capital"`
Now time.Time `firestore:"pop"` // in millions
}
//client.Collection(collection).NewDoc().
wr, err := client.Collection(collection).NewDoc().Create(ctx, State{
Capital: "Denver",
Now: time.Now(),
})
if err != nil {
fmt.Printf("failed to create document: %v\n", err)
}
fmt.Println(wr.UpdateTime)
//// Declared an empty map interface
//var result map[string]interface{}
//
//// Unmarshal or Decode the JSON to the interface.
//json.Unmarshal([]byte(string(document)), &result)
//_, _, err = client.Collection(collection).Add(ctx, wr)
//if err != nil {
// log.Fatalf("Failed adding document to collection %q: %v", collection, err)
//}
iter := client.Collection(collection).Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
fmt.Println(doc.Data())
}
}