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
| Name | Type | Description |
|---|---|---|
connectionString | string | MongoDB connection URI |
Returns
MongoDBClient: Client instance on successnil: 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
| Name | Type | Description |
|---|---|---|
connectionString | string | MongoDB connection URI |
options | table | Connection options |
Options Table
| Option | Type | Default | Description |
|---|---|---|---|
app_name | string | "gmsv_mongo_v2" | Application name for server logs |
max_pool_size | number | 100 | Maximum connections in pool |
retry_writes | boolean | true | Retry failed write operations |
Returns
MongoDBClient: Client instance on successnil: 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
| Name | Type | Description |
|---|---|---|
suppress | boolean | true 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")
-- ...