Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions mongodb/golang/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

// Person -
type Person struct {
Name string
Age int
Expand All @@ -19,7 +20,7 @@ type Person struct {
func main() {

// Set client options
clientOptions := options.Client().ApplyURI("mongodb://mongodb:27017")
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")

// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
Expand All @@ -44,14 +45,14 @@ func main() {
frankie := Person{"Frankie", 31, "Nairobi"}

// Insert a single document
insertResult, err := collection.InsertOne(context.TODO(), ash)
insertResult, err := collection.InsertOne(context.TODO(), ruan)
if err != nil {
log.Fatal(err)
}
fmt.Println("Inserted a single document: ", insertResult.InsertedID)

// Insert multiple documents
trainers := []interface{}{misty, brock}
trainers := []interface{}{james, frankie}

insertManyResult, err := collection.InsertMany(context.TODO(), trainers)
if err != nil {
Expand All @@ -60,11 +61,11 @@ func main() {
fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs)

// Update a document
filter := bson.D{{"name", "Ash"}}
filter := bson.D{{Key: "name", Value: "Frankie"}}

update := bson.D{
{"$inc", bson.D{
{"age", 1},
{Key: "$inc", Value: bson.D{
{Key: "age", Value: 1},
}},
}

Expand Down Expand Up @@ -98,15 +99,15 @@ func main() {
// Iterate through the cursor
for cur.Next(context.TODO()) {
var elem Person
err := cur.Decode(&elem)
if err != nil {
log.Fatal(err)
inerr := cur.Decode(&elem)
if inerr != nil {
log.Fatal(inerr)
}

results = append(results, &elem)
}

if err := cur.Err(); err != nil {
if err = cur.Err(); err != nil {
log.Fatal(err)
}

Expand Down