Created
December 25, 2018 21:32
-
-
Save mbchoa/a9032d05198c1f76a680c94d6bc8f290 to your computer and use it in GitHub Desktop.
Example usage of MongoDB's official Go Driver with nested struct types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"github.com/mongodb/mongo-go-driver/bson" | |
"github.com/mongodb/mongo-go-driver/mongo" | |
"github.com/mongodb/mongo-go-driver/mongo/options" | |
) | |
type Exercise struct { | |
Type string `bson:"type"` | |
Weight float32 `bson:"weight"` | |
Set []int `bson:"set"` | |
} | |
type Workout struct { | |
Exercises []Exercise `bson:"exercises"` | |
} | |
func main() { | |
// 1. Establish connection with Mongo DB | |
client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Check the connection | |
err = client.Ping(context.TODO(), nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Connected to MongoDB!") | |
collection := client.Database("test").Collection("trainers") | |
// 2. Implement CRUD operations on DB | |
squatExercise := models.Exercise{"squat", 200, []int{5, 5, 5}} | |
benchExercise := models.Exercise{"bench", 185, []int{5, 5, 5}} | |
workout1 := models.Workout{Exercises: []models.Exercise{squatExercise, benchExercise}} | |
squatEx2 := models.Exercise{"squat", 205, []int{5, 5, 5}} | |
benchEx2 := models.Exercise{"bench", 190, []int{5, 5, 5}} | |
workout2 := models.Workout{Exercises: []models.Exercise{squatEx2, benchEx2}} | |
squatEx3 := models.Exercise{"squat", 210, []int{5, 5, 5}} | |
benchEx3 := models.Exercise{"bench", 195, []int{5, 5, 5}} | |
workout3 := models.Workout{Exercises: []models.Exercise{squatEx3, benchEx3}} | |
//////////////////////////////////////////////////////////// | |
// // | |
// Insert single document using collection.InsertOne() // | |
// // | |
//////////////////////////////////////////////////////////// | |
insertResult, err := collection.InsertOne(context.TODO(), workout1) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Inserted a single document: ", insertResult.InsertedID) | |
// Insert multiple documents using collection.InsertMany() | |
workouts := []interface{}{workout2, workout3} | |
insertManyResults, err := collection.InsertMany(context.TODO(), workouts) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Inserted multiple documents: ", insertManyResults.InsertedIDs) | |
/////////////////////////////////////////////////////////////////////////////// | |
// // | |
// Find single document using collection.FindOne // | |
// Reference: https://stackoverflow.com/questions/53816611/ // | |
// golang-mongo-go-driver-beta-1-using-greater-than-operator // | |
// // | |
/////////////////////////////////////////////////////////////////////////////// | |
objectID, _ := primitive.ObjectIDFromHex("5c1f555640722cc207956c86") | |
filter := bson.M{"_id": objectID} | |
var result models.Workout | |
err = collection.FindOne(context.TODO(), filter).Decode(&result) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Found a single document: %+v\n", result) | |
////////////////////////////////////////////////////////// | |
// // | |
// Find multiple documents using collection.Find // | |
// // | |
////////////////////////////////////////////////////////// | |
options := options.Find() | |
cur, err := collection.Find(context.TODO(), nil, options) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer cur.Close(context.TODO()) | |
count := 0 | |
var results []*models.Workout | |
for cur.Next(context.TODO()) { | |
// create a value into which the single document can be decoded | |
var elem models.Workout | |
count++ | |
err := cur.Decode(&elem) | |
if err != nil { | |
log.Fatal(err) | |
} | |
results = append(results, &elem) | |
} | |
for _, exercise := range results { | |
stringJson, _ := json.Marshal(*exercise) | |
fmt.Printf("JSON string: %s", stringJson) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment