API Reference

MongoDB Global

Global MongoDB table with client creation and utilities

MongoDB Global

The MongoDB global table is the main entry point for gmsv_mongo. It provides client creation methods and utility functions.

Client

Creates a new MongoDB client with default settings.

Signature

MongoDB.Client(connectionString) → MongoDBClient | nil

Parameters

NameTypeDescription
connectionStringstringMongoDB connection URI

Returns

  • MongoDBClient: Client instance on success
  • nil: On connection failure

Connection String Format

mongodb://[username:password@]host[:port][/database][?options]
mongodb+srv://[username:password@]host[/database][?options]

Examples

-- Local connection
local client = MongoDB.Client("mongodb://localhost:27017")

-- With authentication
local client = MongoDB.Client("mongodb://user:pass@localhost:27017")

-- MongoDB Atlas
local client = MongoDB.Client("mongodb+srv://user:pass@cluster.mongodb.net")

-- Error handling
local client = MongoDB.Client("mongodb://localhost:27017")
if not client then
    print("Connection failed!")
    return
end

ClientWithOptions

Creates a new MongoDB client with custom options.

Signature

MongoDB.ClientWithOptions(connectionString, options) → MongoDBClient | nil

Parameters

NameTypeDescription
connectionStringstringMongoDB connection URI
optionstableConnection options

Options Table

OptionTypeDefaultDescription
app_namestring"gmsv_mongo_v2"Application name for server logs
max_pool_sizenumber100Maximum connections in pool
retry_writesbooleantrueRetry failed write operations

Returns

  • MongoDBClient: Client instance on success
  • nil: On connection failure

Examples

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

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

Version

Returns the module version string.

Signature

MongoDB.Version() → string

Returns

  • string: Version number (e.g., "2.0.0")

Example

print("gmsv_mongo version:", MongoDB.Version())
-- Output: gmsv_mongo version: 2.0.0

SuppressMessages

Controls whether informational messages are printed to console.

Signature

MongoDB.SuppressMessages(suppress)

Parameters

NameTypeDescription
suppressbooleantrue to suppress, false to enable

Example

-- Suppress module messages
MongoDB.SuppressMessages(true)

-- Re-enable messages
MongoDB.SuppressMessages(false)

Usage Pattern

require("mongo")

-- Check version
print("Using gmsv_mongo", MongoDB.Version())

-- Create connection
local client = MongoDB.ClientWithOptions("mongodb://localhost:27017", {
    app_name = "MyServer",
    max_pool_size = 50
})

if not client then
    error("Failed to connect to MongoDB!")
end

-- Suppress further messages
MongoDB.SuppressMessages(true)

-- Use the client
local db = client:Database("mydb")
-- ...