Connect to mongo
This commit is contained in:
1 parent
004641df4e
commit
b069ee1aa2
4 files changed
+61
-7
No files matched your search
@@ -1,15 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"school-management/models"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var student models.Student
|
||||
student.FirstName = "Braeden"
|
||||
student.LastName = "Sowinski"
|
||||
student.Age = 16
|
||||
student.DOB = "2005-04-26" //year-month-day
|
||||
fmt.Print(student)
|
||||
godotenv.Load(".env")
|
||||
mongoURI := os.Getenv("mongoURI")
|
||||
dbo := os.Getenv("dbo")
|
||||
|
||||
// Connect to mongo
|
||||
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
err = client.Connect(ctx)
|
||||
|
||||
// Get collection
|
||||
collection := client.Database(dbo).Collection("students")
|
||||
// res, insertErr := collection.InsertOne(ctx, student)
|
||||
// if insertErr != nil {
|
||||
// log.Fatal(insertErr)
|
||||
// }
|
||||
// fmt.Println(res)
|
||||
|
||||
cur, currErr := collection.Find(ctx, bson.D{})
|
||||
if currErr != nil {
|
||||
panic(currErr)
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
|
||||
var students []models.Student
|
||||
if err = cur.All(ctx, &students); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(students)
|
||||
}
|
||||
Reference in new issue
Block a user