Skip to main content
Version: current

MongoDB

Document Databasebackend/mongodb

MongoDB is a popular NoSQL document database that provides high performance, high availability, and easy scalability. It stores data in flexible, JSON-like documents with dynamic schemas.

📦 Installation

go get github.com/azrod/kivigo/backend/mongodb

Requires a running MongoDB instance. You can use MongoDB Atlas (cloud), run locally, or use Docker: `docker run -d -p 27017:27017 mongo:latest`

Dependencies

  • MongoDB server (4.4 or later recommended)
  • Network connectivity to MongoDB instance
  • Valid connection string with authentication if required

✨ Features

Basic Operations
Batch Operations
Health Checks
ACID Transactions
Replication
Sharding
Indexing
Aggregation Pipeline

🚀 Usage

package main

import (
"context"
"time"

"github.com/azrod/kivigo"
"github.com/azrod/kivigo/backend/mongodb"
"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
// Basic configuration
opt := mongodb.NewOptions()
opt.ConnectionURI = "mongodb://localhost:27017"
opt.Database = "kivigo"
opt.Collection = "keyvalue"

// Advanced configuration with authentication
authOpt := mongodb.NewOptions()
authOpt.ConnectionURI = "mongodb://username:password@mongodb-server:27017/kivigo?authSource=admin"
authOpt.Database = "kivigo"
authOpt.Collection = "documents"

// Optional: Configure connection pool and timeouts
authOpt.MaxPoolSize = 100
authOpt.ConnectTimeout = 10 * time.Second
authOpt.SocketTimeout = 30 * time.Second

// Optional: Configure write concern and read preference
authOpt.WriteConcern = options.WriteConcern{
W: "majority",
J: true,
}
authOpt.ReadPreference = options.ReadPreference{
Mode: "primaryPreferred",
}

// Create backend
kvStore, err := mongodb.New(authOpt)
if err != nil {
panic(err)
}
defer kvStore.Close()

// Create client
client, err := kivigo.New(kvStore)
if err != nil {
panic(err)
}
}

📝 Notes

  • MongoDB stores documents in BSON format, which supports rich data types
  • Use proper indexing strategies for frequently queried fields
  • Consider sharding for horizontal scaling beyond single server capacity
  • MongoDB transactions are available across replica sets and sharded clusters
  • Use MongoDB Compass for visual database management and query optimization
  • Monitor oplog size for replica sets to prevent synchronization issues
  • GridFS can be used for storing files larger than 16MB document limit
  • Connection pooling is essential for high-performance applications

🔗 Additional Resources