# Mongo DB in golang **Published by:** [Primrose](https://paragraph.com/@primrose/) **Published on:** 2023-08-18 **URL:** https://paragraph.com/@primrose/mongo-db-in-golang ## Content MongoDBMongoDB는 C++로 작성된 대표적인 NoSQL이다. 보통 AWS Document DB나 Mongo Atlas를 이용하곤하는데, 본 예제에서는 Mongo Atlas를 이용했다. 공식 문서에서는 Document oriented database라고 소개하고 있는데, 여기서 말하는 Document는 RDMS의 record와 비슷한 개념이다.{ "_id": ObjectId("5099803df3f4948bd2f98391"), "username": "velopert", "name": { first: "M.J.", last: "Kim" } } 위와 같은 json 형태의 데이터가 있다고 생각해보자. 실제로 MongoDB에 어떤 데이터를 저장하면 위와 같이 생성되는 것을 확인할 수 있다. Document는 어떤 형태의 데이터를 문서로 보는 관점이라고 생각하면 좋을 것 같다.CollectionCollection은 Document의 그룹이다. RDMS의 테이블과 비슷하다고 볼 수 있지만 조금 다른게, Document 자체는 동적인 schema를 가지고 있다. (schema가 없다) 간단하게 생각하면 다음과 같은 일이 가능하다.Collection 1 { { "_id": ObjectId("5099803df3f4948bd2f98391"), "username": "velopert", "name": { first: "M.J.", last: "Kim" } }, { "_id": ObjectId("5099803df3f4948bd2f98391"), "name": { first: "M.J.", last: "Kim" } } } 같은 Collection에서 각기 다른 json 스키마를 허용한다.DatabaseMongoDB Atlas에서 Database를 생성하고, 그 이후에 Collection을 생성할 수가 있는데, Database는 Collection들의 물리적인 컨테이너이다.UsageMongo DB Atlas를 들어가보고 느낀건데, 정말 잘 되어있다. 코드 예제, 클러스터 생성부터 개발자 친화적으로 잘 만들어줬다는 생각이 들었다.또한 Mongo Atlas를 이용해서 AWS에 서버리스 형태로 배포하더라도 Document DB를 쓰는 것 보다 요금이 싼 것 같다.서버리스 형태로 AWS에 클러스터를 생성할 수 있었고, Go에서는 다음과 같이 연결할 수 있다.package db import ( "context" "fmt" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "os" ) func MongoURI() string { return fmt.Sprintf("mongodb+srv://%s:%s@%s.subnw.mongodb.net/?retryWrites=true&w=majority", os.Getenv("MONGODB_USERNAME"), os.Getenv("MONGODB_PASSWORD"), os.Getenv("MONGODB_CLUSTER")) } func MongoOpts() *options.ServerAPIOptions { return options.ServerAPI(options.ServerAPIVersion1) } func NewMongoDB() *mongo.Client { serverAPI := MongoOpts() uri := MongoURI() opts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPI) client, err := mongo.Connect(context.Background(), opts) if err != nil { panic(err) } return client } 꽤나 단순하게 연결이 가능하다. 예제 코드는 전부 MongoDB Atlas에서 제공해준 코드이다.ServerAPIVersion1 이라는 코드를 보면 왜 버전1을 쓴거지? 라는 생각이 들 수 있지만 Version1밖에 없고 Stable이라고 하니 안심하고 써도 될 것 같다.Sourcehttps://velopert.com/436 ## Publication Information - [Primrose](https://paragraph.com/@primrose/): Publication homepage - [All Posts](https://paragraph.com/@primrose/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@primrose): Subscribe to updates