|
1 | 1 | local Mongo = exports["fivemongo"] |
2 | 2 |
|
| 3 | + |
| 4 | +local Mongo |
| 5 | +RegisterCommand('getusers', function(source, args, rawCommand) |
| 6 | + Mongo:FindMany({ |
| 7 | + collection = "users", |
| 8 | + query = {} |
| 9 | + }, function(err, users) |
| 10 | + if not users or #users == 0 then |
| 11 | + print("^1No users found.^7") |
| 12 | + return |
| 13 | + end |
| 14 | + |
| 15 | + for _, user in ipairs(users) do |
| 16 | + print(("User: %s | Last Login: %s"):format(user.username, user.last_login or "Never")) |
| 17 | + end |
| 18 | + end) |
| 19 | +end, false) |
| 20 | + |
| 21 | + |
| 22 | +Mongo:FindOne({ |
| 23 | + collection = "developers", |
| 24 | + query = { steamhex = identifier } |
| 25 | +}, function(err, existingUser) |
| 26 | + if (err) then |
| 27 | + print("Error searching developer whitelist:", err) |
| 28 | + return cb(false) |
| 29 | + end |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + if (existingUser) then |
| 34 | + -- User found in whitelist |
| 35 | + print("User already in whitelist:", json.encode(existingUser)) |
| 36 | + cb(true) -- Already whitelisted, treat as success |
| 37 | + elseif (not existingUser) then |
| 38 | + -- User not found, insert new whitelist entry |
| 39 | + Mongo:InsertOne({ |
| 40 | + collection = "developers", |
| 41 | + document = { |
| 42 | + steamhex = identifier, |
| 43 | + addedAt = os.date("!%Y-%m-%dT%H:%M:%SZ") -- ISO 8601 UTC timestamp |
| 44 | + } |
| 45 | + }, function(err, result) |
| 46 | + if (err) then |
| 47 | + print("Failed to add user to developer list:", identifier, err) |
| 48 | + cb(false) |
| 49 | + else |
| 50 | + print("User added to developer list:", identifier) |
| 51 | + cb(true) |
| 52 | + end |
| 53 | + end) |
| 54 | + end |
| 55 | +end) |
| 56 | + |
| 57 | + |
| 58 | +Mongo:InsertMany({ |
| 59 | + collection = "users", |
| 60 | + documents = { |
| 61 | + { username = "user1", email = "user1@example.com" }, |
| 62 | + { username = "user2", email = "user2@example.com" } |
| 63 | + } |
| 64 | +}, function(err, result) |
| 65 | + if err then |
| 66 | + print("InsertMany Error:", err) |
| 67 | + else |
| 68 | + print("Inserted documents:", json.encode(result.insertedIds)) |
| 69 | + end |
| 70 | +end) |
| 71 | + |
| 72 | + |
| 73 | +Mongo:UpdateOne({ |
| 74 | + collection = "users", |
| 75 | + query = { username = "jane_doe" }, |
| 76 | + update = { |
| 77 | + ["$set"] = { last_login = os.date("!%Y-%m-%dT%H:%M:%SZ") } or last_login = os.date("!%Y-%m-%dT%H:%M:%SZ") |
| 78 | + } |
| 79 | +}, function(err, result) |
| 80 | + if err then |
| 81 | + print("UpdateOne Error:", err) |
| 82 | + else |
| 83 | + print("Modified count:", result.modifiedCount) |
| 84 | + end |
| 85 | +end) |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +Mongo:UpdateMany({ |
| 90 | + collection = "users", |
| 91 | + query = { active = false }, |
| 92 | + update = { |
| 93 | + ["$set"] = { active = true } |
| 94 | + } |
| 95 | +}, function(err, result) |
| 96 | + if err then |
| 97 | + print("UpdateMany Error:", err) |
| 98 | + else |
| 99 | + print("Documents updated:", result.modifiedCount) |
| 100 | + end |
| 101 | +end) |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +Mongo:DeleteOne({ |
| 107 | + collection = "users", |
| 108 | + query = { username = "user_to_delete" } |
| 109 | +}, function(err, result) |
| 110 | + if err then |
| 111 | + print("DeleteOne Error:", err) |
| 112 | + else |
| 113 | + print("Deleted count:", result.deletedCount) |
| 114 | + end |
| 115 | +end) |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +Mongo:DeleteMany({ |
| 121 | + collection = "users", |
| 122 | + query = { inactive = true } |
| 123 | +}, function(err, result) |
| 124 | + if err then |
| 125 | + print("DeleteMany Error:", err) |
| 126 | + else |
| 127 | + print("Deleted documents:", result.deletedCount) |
| 128 | + end |
| 129 | +end) |
| 130 | + |
| 131 | + |
| 132 | +Mongo:Count({ |
| 133 | + collection = "users", |
| 134 | + query = { active = true } |
| 135 | +}, function(err, count) |
| 136 | + if err then |
| 137 | + print("CountDocuments Error:", err) |
| 138 | + else |
| 139 | + print("Active users count:", count) |
| 140 | + end |
| 141 | +end) |
| 142 | + |
| 143 | + |
| 144 | +Mongo:FindSpec({ |
| 145 | + collection = "users", |
| 146 | + query = { age = { ["$gt"] = 18 } }, |
| 147 | + limit = 50 |
| 148 | +}, function(err, data) |
| 149 | + if err then |
| 150 | + print("Error fetching users:", err) |
| 151 | + return |
| 152 | + end |
| 153 | + |
| 154 | + for _, user in ipairs(data) do |
| 155 | + print(("User: %s | Identifier: %s"):format(user.username, user.identifier)) |
| 156 | + end |
| 157 | +end) |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +-- TODO! |
| 162 | +Mongo:Aggregate({ |
| 163 | + collection = "users", |
| 164 | + pipeline = { |
| 165 | + { ["$match"] = { active = true } }, |
| 166 | + { ["$group"] = { |
| 167 | + _id = "$country", |
| 168 | + count = { ["$sum"] = 1 } |
| 169 | + }} |
| 170 | + } |
| 171 | +}, function(err, results) |
| 172 | + if err then |
| 173 | + print("Aggregate Error:", err) |
| 174 | + else |
| 175 | + for _, row in ipairs(results) do |
| 176 | + print(("Country: %s | Users: %d"):format(row._id, row.count)) |
| 177 | + end |
| 178 | + end |
| 179 | +end) |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + |
3 | 186 | RegisterCommand("addproduct", function(source, args, rawCommand) |
4 | 187 | local productName = args[1] or "Red Car" |
5 | 188 | local randomPrice = math.random(1000, 5000) |
|
0 commit comments