HomeArtTechHackBlockchain
ONLINE ·
Index
/
Technology
/
Article

Docker compose ทำ mongodb มาเชื่อมต่อ กับ golang

Operator
Khomkrid Lerdprasert
Filed
March 05, 2022
Channel
Technology
Read
~1 min
Docker compose ทำ mongodb มาเชื่อมต่อ กับ golang

##Docker compose ทำ mongodb มาเชื่อมต่อ กับ golang

version: "3.7"
services:
database:
image: mongo:4.4
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=dd_hakka
- MONGO_INITDB_USERNAME=dd_hakka
- MONGO_INITDB_PASSWORD=password
volumes:
- ./init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh
- ./db:/data/db
package main
import (
"context"
"log"
"time"
"github.com/aofiee/mongodb/handler"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type (
Podcast struct {
PodcastName string `bson:"podcast_name"`
}
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://dd_hakka:password@localhost:27017/dd_hakka"))
defer func() {
if err = client.Disconnect(ctx); err != nil {
panic(err)
}
}()
db := client.Database("dd_hakka")
log.Println(`Connected to db_hakka`, db)
podcastsCollection := db.Collection("podcasts")
log.Println(`Created collection podcasts`, podcastsCollection)
podcast := Podcast{
PodcastName: "The Go Programming Language",
}
log.Println(`Inserting podcast`, podcast)
podcastResult, err := podcastsCollection.InsertOne(ctx, podcast)
if err != nil {
log.Println("Error inserting podcast: ", err)
}
log.Println("Inserted podcast with ID: ", podcastResult.InsertedID)
}

◎ Tags

##docker##golang
Khomkrid Lerdprasert
Operator

Khomkrid Lerdprasert

Technical Lead — building AI-powered platforms, omni-channel chat systems, and telemedicine solutions with Go, Next.js & clean architecture. 20+ years shipping software from crypto wallets to e-learning systems. Bangkok-based. Writes code late at night, brews beer on weekends.

GithubInstagram
Previous · transmission
บันทึก การทำ Firebase Messaging ด้วย Golang และ Config ให้ใช้งานบน iOS
Next · transmission
บันทึก การทำ Hexagonal - gofiber - graphQL - Postgres จ่ะ
Metadata
Channel
Technology
Filed
March 05, 2022
Read
~1 min
Language
TH / EN
Transmit

Related

Basic music streaming with go fiber
Khomkrid Lerdprasert
October 08, 2021
1 min
aofiee.dev
signal / noise / v4
© 2019 – 2026, Khomkrid Lerdprasert.
All transmissions logged.
No newsletter. No cookies. No tracker.
PGP · 7F3D 2024 A21E B584 · 0x7F3D
Channels
  • Art & Culture
  • Technology
  • Hack 101
  • Blockchain 101
  • Archive / All posts
— END OF TRANSMISSION —
// powered by curiosity, coffee, & wuxia
BKK · 13°45′N · 100°30′E