/
mgo-ref.go
78 lines (69 loc) · 1.67 KB
/
mgo-ref.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
71
72
73
74
75
76
77
78
// vim: ts=2 sw=2 sts=2 sta ai si noci
package main
import (
"encoding/json"
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
"os"
)
type Author struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Name string `bson:"t" json:"name"`
}
type Book struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Title string `bson:"t" json:"title"`
AuthorIds []bson.ObjectId `bson:"AuthorIds" json:"authorids"`
}
type AssembledBooks struct {
Book
Authors []Author
}
func main() {
logout := log.New(os.Stdout, "MGO: ", log.Lshortfile)
mgo.SetLogger(logout)
mgo.SetDebug(false)
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
db := session.DB("bookdb")
cbooks := db.C("bookcoll")
cauthors := db.C("authorcoll")
aids := []bson.ObjectId{bson.NewObjectId(), bson.NewObjectId()}
authors := []Author{{
aids[0],
"Author 1",
}, {
aids[1],
"Author 2",
}}
cauthors.Insert(authors[0])
cauthors.Insert(authors[1])
// Insert some books
mine := Book{
bson.NewObjectId(),
"Gang of four thingy",
aids,
}
cbooks.Insert(&mine)
var assembl []AssembledBooks
cauthors.Find(bson.M{}).All(&assembl)
str1, _ := json.MarshalIndent(assembl, "", " ")
fmt.Printf("%s\n", str1)
var allauthors []Author
cauthors.Find(bson.M{}).All(&allauthors)
str, _ := json.MarshalIndent(allauthors, "", " ")
fmt.Printf("%s\n", str)
var allbooks []Book
cbooks.Find(bson.M{}).All(&allbooks)
str, _ = json.MarshalIndent(allbooks, "", " ")
fmt.Printf("%s\n", str)
fmt.Println("Dropping all collections...")
cauthors.DropCollection()
cbooks.DropCollection()
fmt.Println("Done")
}