This resource is a simple MongoDB wrapper for FiveM. It's running on top of MongoDB Node Driver.
- Clone this repository to
resources/mongodb
folder. - Copy
mongodb/database.cfg
to your server root directory. - Add the following lines to your server config:
exec "database.cfg"
start mongodb
- Change
mongodb_url
andmongodb_database
indatabase.cfg
. - Run
npm install
inresources/mongodb
directory.
Every callback accepts success<boolean>
as its first argument. If success
is false
, second argument contains error message.
Example (Lua):
exports.mongodb:findOne({ collection = "users", query = { _id = id } }, function (success, result)
if not success then
print("Error message: "..tostring(result))
return
end
print("User name is "..tostring(result[1].name))
end)
#Aggregate
-- how to use
exports.mongodb:aggregate({
collection = "users",
pipeline = {
[1] = { ["$match"] = { identifier = "steam:110000109bbb53b" } },
[2] = { ["$project"] = { playerName = 1 } } ,
[3] = { ["$limit"] = 1 }
}
}, function (success,result)
if not success then
print("Error message: "..tostring(result))
else
for _, user in pairs(result) do
for key, value in pairs(user) do
print(key, " : " , value)
end
print("-------")
end
end
end)
#Find
exports.mongodb:find({
collection = "phone_message",
query = {
transmitter = "",
receiver = ""
},
limit = 100,
sort = {
time = -1
}
}, function (success, result)
if not success then
return
end
print("\n** 100 message lastest")
for i, document in ipairs(result) do
for k, v in pairs(document) do
print("* "..tostring(k).." = \""..tostring(v).."\"")
end
end
end)
#CreateIndex
AddEventHandler("onDatabaseConnect", function (databaseName)
exports.mongodb:createIndex({
collectionName = "esx_status",
keys = { ["identifier"] = 1},
options= { ["unique"] = true , ["name"] = "identifier" }
})
end)