Connection

Connection Options

Configure advanced connection options

Connection Options

Use MongoDB.ClientWithOptions() for advanced connection configuration.

Basic Usage

local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    app_name = "MyGModServer",
    max_pool_size = 50,
    retry_writes = true
})

Available Options

app_name

Application name for server monitoring and logging.

local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    app_name = "MyGameServer"
})
  • Type: string
  • Default: "gmsv_mongo_v2"
  • Purpose: Appears in MongoDB server logs and monitoring tools

max_pool_size

Maximum number of connections in the connection pool.

local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    max_pool_size = 100
})
  • Type: number
  • Default: 100
  • Purpose: Controls concurrent connection capacity

retry_writes

Automatically retry failed write operations.

local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    retry_writes = true
})
  • Type: boolean
  • Default: true
  • Purpose: Handles transient network failures gracefully

Complete Options Example

local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    -- Application identification
    app_name = "MyGModServer",

    -- Connection pool configuration
    max_pool_size = 50,

    -- Retry behavior
    retry_writes = true
})

if client then
    print("✓ Connected with custom options!")
end

Internal Configuration

The library uses these internal defaults (from Rust configuration):

SettingDefault ValueDescription
min_pool_size10Minimum connections maintained
max_pool_size100Maximum connections allowed
server_selection_timeout30sTime to select a server
connect_timeout10sConnection establishment timeout
max_idle_time600sClose idle connections after
retry_writestrueRetry failed writes
retry_readstrueRetry failed reads

Connection Pool Behavior

How Pooling Works

  1. Initial Connection: First operation creates connection(s)
  2. Reuse: Subsequent operations reuse existing connections
  3. Scaling: Pool grows based on demand (up to max_pool_size)
  4. Cleanup: Idle connections are closed after max_idle_time

Pool Size Guidelines

-- Development/Testing
local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    max_pool_size = 10
})

-- Small server (< 32 players)
local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    max_pool_size = 25
})

-- Medium server (32-64 players)
local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    max_pool_size = 50
})

-- Large server (64+ players)
local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    max_pool_size = 100
})

Environment-Specific Examples

Development

local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    app_name = "GModDev",
    max_pool_size = 10
})

Production

local client = MongoDB.ClientWithOptions("mongodb://prod-user:secure-pass@mongo.example.com:27017", {
    app_name = "GModProd_Server1",
    max_pool_size = 75,
    retry_writes = true
})

Docker Environment

-- When MongoDB is in the same Docker network
local client = MongoDB.ClientWithOptions("mongodb://mongo:27017", {
    app_name = "GModDocker",
    max_pool_size = 50
})

Monitoring Connections

Check Connection Health

local function checkConnection(client)
    if not client then
        return false, "No client"
    end

    local databases = client:ListDatabases()
    if databases then
        return true, "Connected (" .. #databases .. " databases)"
    else
        return false, "Connection test failed"
    end
end

-- Usage
local ok, message = checkConnection(client)
print(ok and "" or "", message)

Connection in Server Logs

With app_name set, you'll see entries in MongoDB logs:

{"t":{"$date":"2024-01-15T10:30:00.000+00:00"},"s":"I",
 "c":"NETWORK","msg":"Connection accepted",
 "attr":{"appName":"MyGModServer"}}

Best Practices

  1. Set app_name: Helps identify connections in MongoDB logs
  2. Size pool appropriately: Match to expected concurrent operations
  3. Enable retry_writes: Handles transient network issues
  4. Use single client: Reuse the same client instance
  5. Test on startup: Verify connection before starting server

Next Steps