A MongoDB module for Garry's Mod!
Windows | Linux |
---|---|
Why does Garry's Mod need a MongoDB module? Because MongoDB is a NoSQL database. MySQL is designed for static schemas and performs best with complex queries.
Typically, the data stored by Garry's Mod servers is extremely dynamic and the queries performed are very simple. This makes MySQL a pain.
With MongoDB you don't have to worry about schemas at all! Just write and query whatever data you want!
Download a pre-built binary for your system from the Releases section.
Place the .dll
in garrysmod/lua/bin
.
For more details see the documentation or examples.
require('mongodb')
-- Note: Connecting to the MongoDB cluster can hang! Cache the result in a global!
client = client or mongodb.Client('<CONNECTION_URI>', '<APP_NAME>')
-- Retrieve databases
local database = client:Database('test')
-- Retrieve collections
local players = database:GetCollection('players')
-- Run queries (Find, Insert, Update, Remove)
local data = players:Find({ name = 'Bob', age = 25 })
print(data[1].name)
players:Update({ name = 'Bob' }, { ['$set'] = { age = 26 } })
-- Execute bulk queries
local bulk = players:Bulk()
for i, ply in ipairs({ 'Bob', 'Jim', 'Ann' }) do
bulk:Insert({ name = ply, age = 20 + i })
end
local result = bulk:Execute()
Code licensed under the MIT License.