Primrose Docs
  • Home
    • ⛓️Blockchain
      • Avalanche
        • What is AVAX?
      • Ethereum
        • Ethereum Cancun Upgrade Explained(draft)
        • go-ethereum: gas estimate
        • Blockchain Transaction Lifecycle
        • Mempool
        • Gas optimization in Solidity, Ethereum
      • Solidity DeepDive
        • Meta transaction
        • solidity: patterns
        • UUPS vs Transparent
        • Solidity Interface
        • Smart contract storage
        • ERC-2981 Contract
        • Solidity modifier
        • Solidity delete keyword
        • How To Make NFTs with On-Chain Metadata - Hardhat and JavaScript
        • How to Build "Buy Me a Coffee" DeFi dapp
        • How to Develop an NFT Smart Contract (ERC 721) with Alchemy
        • Upgradeable Contract
        • Smart Contract Verification
      • Common
        • Eigenlayer
        • MultiSig(draft)
        • Chain-Based Proof-of- Stake, BFT-Style Proof-of-Stake
        • Byzantine Fault Tolerance
        • Zero-knowledge
        • Hierarchical Deterministic Wallet
        • Maker DAO
        • Defi
        • Uniswap
        • IBC
        • Cosmos
        • Gossip Protocol
        • Tendermint
        • UTXO vs Account
        • Blockchain Layer
        • Consensus Algorithm
        • How does mining work?
        • Immutable Ledger
        • SHA256 Hash
        • Filecoin
        • IPFS - InterPlanetary File System
        • IPFS와 파일코인
        • Livepeer
        • Layer 0
      • Bitcoin
        • BIP for HD Wallet
        • P2WPKH
        • Segwit vs Native Segwit
    • 📖Languages
      • Javascript/Typescript
        • Hoisting
        • This value in Javascript
        • Execution Context
        • About Javscript
        • tsconfig.json
        • Nest js Provider
        • 'return await promise' vs 'return promise'
      • Python
        • Pythonic
        • Python: Iterable, Iterator
        • Uvicorn & Gunicorn
        • WSGI, ASGI
        • Python docstring
        • Decorator in Python
        • Namespace in Python
        • Python Method
      • Go
        • GORM+MySQL Connection Pool
        • Context in golang
        • How to sign Ethereum EIP-1559 transactions using AWS KMS
        • Mongo DB in golang(draft)
        • Golang HTTP Package
        • Panic
        • Golang new/make
        • golang container package
        • errgroup in golang
        • Generic Programming in Golang
        • Goroutine(draft)
    • 📝Database
      • MongoDB in golang
      • Nested loop join, Hash join
      • DB Query plan
      • Index
      • Optimistic Lock Pessimistic Lock
    • 💻Computer Science
      • N+1 query in go
      • Web server 를 구성할 때 Thread, Process 개수를 어떻게 정할 것인가?
      • CAP
      • Socket programming
      • DNS, IP
      • URL, URI
      • TLS과 SSL
      • Caching(draft)
      • Building Microservices: Micro Service 5 Deploy Principle
      • Red Black Tree
      • AOP
      • Distributed Lock
      • VPC
      • Docker
      • All about Session and JWT
      • Closure
      • Singleton Pattern
      • TCP 3 way handshake & 4 way handshake
      • Race Condition
      • Process Address Space 
      • Call by value, Call by reference, Call by assignment
      • Zookeeper, ETCD
      • URL Shortening
      • Raft consensus
      • Sharding, Partitioning
    • 📒ETC
      • K8S SIGTERM
      • SQS
      • Git Branch Strategy: Ship / Show / Ask
      • Kafka
      • Redis Data Types
      • CI/CD
      • How does Google design APIs?
      • Minishell (42 cursus)
      • Coroutine & Subroutine
      • Redis
Powered by GitBook
On this page
  • MongoDB
  • Collection
  • Database
  • Usage
  • Source
  1. Home
  2. Languages
  3. Go

Mongo DB in golang(draft)

MongoDB

MongoDB는 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는 어떤 형태의 데이터를 문서로 보는 관점이라고 생각하면 좋을 것 같다.

Collection

Collection은 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 스키마를 허용한다.

Database

MongoDB Atlas에서 Database를 생성하고, 그 이후에 Collection을 생성할 수가 있는데, Database는 Collection들의 물리적인 컨테이너이다.

Usage

Mongo 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이라고 하니 안심하고 써도 될 것 같다.

Source

PreviousHow to sign Ethereum EIP-1559 transactions using AWS KMSNextGolang HTTP Package

Last updated 1 year ago

📖