Skip to content

dhkatz/gmsv_mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gmsv_mongodb

A MongoDB module for Garry's Mod!

Windows Linux
Build status Build Status

Quickstart

Information

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

Download a pre-built binary for your system from the Releases section.

Place the .dll in garrysmod/lua/bin.

Usage

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()

License

Code licensed under the MIT License.