/
main.go
79 lines (65 loc) · 1.65 KB
/
main.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
79
package main
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2/bson"
)
// Movie holds a movie data
type Movie struct {
Name string `bson:"name"`
Year string `bson:"year"`
Directors []string `bson:"directors"`
Writers []string `bson:"writers"`
BoxOffice `bson:"boxOffice"`
}
// BoxOffice is nested in Movie
type BoxOffice struct {
Budget uint64 `bson:"budget"`
Gross uint64 `bson:"gross"`
}
func main() {
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
panic(err)
}
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB successfully")
collection := client.Database("appDB").Collection("movies")
// Create a movie
darkNight := Movie{
Name: "The Dark Knight",
Year: "2008",
Directors: []string{"Christopher Nolan"},
Writers: []string{"Jonathan Nolan", "Christopher Nolan"},
BoxOffice: BoxOffice{
Budget: 185000000,
Gross: 533316061,
},
}
// Insert a document into MongoDB
_, err = collection.InsertOne(context.TODO(), darkNight)
if err != nil {
log.Fatal(err)
}
queryResult := &Movie{}
// bson.M is used for nested fields
filter := bson.M{"boxOffice.budget": bson.M{"$gt": 150000000}}
result := collection.FindOne(context.TODO(), filter)
err = result.Decode(queryResult)
if err != nil {
log.Fatal(err)
}
fmt.Println("Movie:", queryResult)
err = client.Disconnect(context.TODO())
if err != nil {
panic(err)
}
fmt.Println("Disconnected from MongoDB")
}