This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathexample.lua
91 lines (83 loc) · 3.18 KB
/
example.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
local function printFiveUsers()
local params = {
collection = "users",
query = {},
limit = 5,
options = {
-- Include username and exclude _id field
projection = {username = 1, _id = 0}
}
}
exports.mongodb:find(params, function (success, result)
if not success then
return
end
print("\n** 5 users")
for i, document in ipairs(result) do
for k, v in pairs(document) do
print("* "..tostring(k).." = \""..tostring(v).."\"")
end
end
end)
end
local function printUser(id)
exports.mongodb:findOne({ collection="users", query = { _id = id } }, function (success, result)
if not success then
return
end
print("\n** User document")
for k, v in pairs(result[1]) do
print("* "..tostring(k).." = \""..tostring(v).."\"")
end
end)
end
AddEventHandler("onDatabaseConnect", function (databaseName)
print("[MongoDB][Example] onDatabaseConnect: "..tostring(databaseName))
local username = "User0"
-- Find user by username
exports.mongodb:findOne({ collection="users", query = { username = username } }, function (success, result)
if not success then
print("[MongoDB][Example] Error in findOne: "..tostring(result))
return
end
-- Print user if already exists
if #result > 0 then
print("[MongoDB][Example] User is already created")
printUser(result[1]._id)
exports.mongodb:updateOne({ collection="users", query = { _id = result[1]._id }, update = { ["$set"] = { first_name = "Bob" } } })
return
end
print("[MongoDB][Example] User does not exist. Creating...")
exports.mongodb:insertOne({ collection="users", document = { username = username, password = "123" } }, function (success, result, insertedIds)
if not success then
print("[MongoDB][Example] Error in insertOne: "..tostring(result))
return
end
print("[MongoDB][Example] User created. New ID: "..tostring(insertedIds[1]))
printUser(insertedIds[1])
end)
end)
exports.mongodb:count({collection="users"}, function (success, result)
if not success then
print("[MongoDB][Example] Error in count: "..tostring(result))
return
end
print("[MongoDB][Example] Current users count: "..tostring(result))
if result < 10 then
local insertUsers = {}
for i = 1, 10 do
table.insert(insertUsers, { username = "User"..i, password = "123456" })
end
exports.mongodb:insert({ collection="users", documents = insertUsers }, function (success, result)
if not success then
print("[MongoDB][Example] Failed to insert users: "..tostring(result))
return
end
print("[MongoDB][Example] Inserted "..tostring(result).." new users")
printFiveUsers()
end)
else
printFiveUsers()
end
end)
end)