API Reference
MongoDBDatabase
Database class for collection management
MongoDBDatabase
The MongoDBDatabase class represents a MongoDB database and provides access to collections and database-level operations.
Collection
Gets a collection instance by name.
Signature
db:Collection(name) → MongoDBCollection
Parameters
| Name | Type | Description |
|---|---|---|
name | string | Collection name |
Returns
MongoDBCollection: Collection instance
Notes
- Collections are created automatically when you first insert data
- Collection names are case-sensitive
Example
local db = client:Database("gameserver")
local players = db:Collection("players")
local items = db:Collection("items")
-- Now use the collections
players:InsertOne({ name = "Test" })
ListCollections
Lists all collections in the database.
Signature
db:ListCollections() → table | nil
Returns
table: Array of collection name stringsnil: On failure
Example
local collections = db:ListCollections()
if collections then
print("Collections in database:")
for i, name in ipairs(collections) do
print(string.format(" %d. %s", i, name))
end
end
CreateCollection
Explicitly creates a new collection.
Signature
db:CreateCollection(name) → boolean
Parameters
| Name | Type | Description |
|---|---|---|
name | string | Collection name to create |
Returns
boolean:trueon success,falseon failure
Notes
- Collections are normally created automatically on first insert
- Use this for explicit creation or when you need specific options
Example
local success = db:CreateCollection("new_collection")
if success then
print("✓ Collection created")
else
print("✗ Failed to create collection")
end
DropCollection
Drops (deletes) a collection and all its documents.
Signature
db:DropCollection(name) → boolean
Parameters
| Name | Type | Description |
|---|---|---|
name | string | Collection name to drop |
Returns
boolean:trueon success,falseon failure
Warning
This permanently deletes the collection and ALL documents in it. This cannot be undone!
Example
-- Confirm before dropping!
local success = db:DropCollection("old_data")
if success then
print("✓ Collection dropped")
end
Stats
Gets statistics for a collection.
Signature
db:Stats(collectionName) → table | nil
Parameters
| Name | Type | Description |
|---|---|---|
collectionName | string | Collection name |
Returns
table: Statistics table with various metricsnil: On failure
Statistics Fields
| Field | Type | Description |
|---|---|---|
count | number | Number of documents |
size | number | Total data size in bytes |
storageSize | number | Storage allocated in bytes |
avgObjSize | number | Average document size |
totalIndexSize | number | Total index size in bytes |
Example
local stats = db:Stats("players")
if stats then
print("Collection Statistics:")
print(" Documents:", stats.count)
print(" Size:", stats.size, "bytes")
print(" Avg Doc Size:", stats.avgObjSize, "bytes")
print(" Index Size:", stats.totalIndexSize, "bytes")
end
Drop
Drops (deletes) the entire database.
Signature
db:Drop() → boolean
Returns
boolean:trueon success,falseon failure
Warning
This permanently deletes the ENTIRE DATABASE and all collections/documents. Use with extreme caution!
Example
-- DANGEROUS - deletes everything!
local success = db:Drop()
if success then
print("Database dropped")
end
Complete Example
require("mongo")
local client = MongoDB.Client("mongodb://localhost:27017")
local db = client:Database("my_game")
-- Create collections
db:CreateCollection("players")
db:CreateCollection("items")
db:CreateCollection("logs")
-- List collections
print("Collections:")
for _, name in ipairs(db:ListCollections() or {}) do
print(" - " .. name)
end
-- Get statistics
local stats = db:Stats("players")
if stats then
print("Players collection has", stats.count, "documents")
end
-- Use collections
local players = db:Collection("players")
players:InsertOne({ username = "Test", level = 1 })
-- Cleanup unused collection
db:DropCollection("logs")