|
1 | 1 | # 🍋 MongoDB Wrapper for FiveM |
2 | 2 |
|
3 | | -> ⚠️ **This resource is currently in testing phase.** Expect frequent updates and improvements. Feedback is welcome! |
| 3 | +> ⚠️ **This resource is in active development.** Bugs and frequent updates are expected. Feedback and contributions are welcome! |
4 | 4 |
|
5 | | -A modern and lightweight **MongoDB wrapper** built for **FiveM** using Lua. It provides a simple and efficient way to interact with MongoDB databases in your server scripts, supporting all the essential operations like `find`, `insert`, `update`, and `delete`. |
| 5 | +A modern, lightweight **MongoDB wrapper** built for **FiveM** using Lua. It provides a simple and efficient way to interact with MongoDB databases inside your server scripts — supporting all essential operations such as `find`, `insert`, `update`, `delete`, and more. |
6 | 6 |
|
7 | 7 | --- |
8 | 8 |
|
9 | 9 | ## 🚀 Features |
10 | 10 |
|
11 | | -- ✅ Simple and readable Lua MongoDB API |
12 | | -- ✅ Asynchronous operations with callback support |
13 | | -- ✅ Support for `ObjectId` conversion |
14 | | -- ✅ ESX/QBCore-friendly |
15 | | -- ✅ Easily extendable for custom use cases |
16 | | -- ✅ Built with scalability in mind |
| 11 | +- ✅ Clean and readable Lua-based API |
| 12 | +- ✅ Asynchronous operations with callback support |
| 13 | +- ✅ Automatic `ObjectId` handling |
| 14 | +- ✅ Compatible with ESX / QBCore |
| 15 | +- ✅ Easily extendable for custom use cases |
| 16 | +- ✅ Built with scalability in mind |
17 | 17 |
|
18 | 18 | --- |
19 | 19 |
|
20 | 20 | ## 📦 Supported Operations |
21 | 21 |
|
22 | | -- `Mongo:Find()` |
23 | | -- `Mongo:FindOne()` |
24 | | -- `Mongo:InsertOne()` |
25 | | -- `Mongo:UpdateOne()` |
26 | | -- `Mongo:DeleteOne()` |
27 | | -- `Mongo:FindMany()` |
28 | | -- `Mongo:FindOneAndDelete()` |
| 22 | +- `Mongo:Find()` |
| 23 | +- `Mongo:FindOne()` |
| 24 | +- `Mongo:InsertOne()` |
| 25 | +- `Mongo:InsertMany()` |
| 26 | +- `Mongo:UpdateOne()` |
| 27 | +- `Mongo:UpdateMany()` |
| 28 | +- `Mongo:DeleteOne()` |
| 29 | +- `Mongo:DeleteMany()` |
| 30 | +- `Mongo:FindMany()` |
| 31 | +- `Mongo:FindOneAndDelete()` |
| 32 | +- `Mongo:Count()` |
| 33 | +- `Mongo:Aggregate() ` *[TODO]* |
| 34 | +- `Mongo:FindSpec()` *(custom utility)* |
29 | 35 |
|
30 | 36 | --- |
31 | 37 |
|
32 | | -## 📚 Example Usage |
| 38 | +## 📚 Example Usages |
| 39 | + |
| 40 | + |
33 | 41 |
|
34 | 42 | ```lua |
35 | 43 | local Mongo |
36 | 44 | RegisterCommand('getusers', function(source, args, rawCommand) |
37 | | - Mongo:Find({ |
| 45 | + Mongo:FindMany({ |
38 | 46 | collection = "users", |
39 | 47 | query = {} |
40 | | - }, function(users) |
| 48 | + }, function(err, users) |
41 | 49 | if not users or #users == 0 then |
42 | 50 | print("^1No users found.^7") |
43 | 51 | return |
@@ -84,6 +92,133 @@ Mongo:FindOne({ |
84 | 92 | end) |
85 | 93 | end |
86 | 94 | end) |
| 95 | + |
| 96 | + |
| 97 | +Mongo:InsertMany({ |
| 98 | + collection = "users", |
| 99 | + documents = { |
| 100 | + { username = "user1", email = "user1@example.com" }, |
| 101 | + { username = "user2", email = "user2@example.com" } |
| 102 | + } |
| 103 | +}, function(err, result) |
| 104 | + if err then |
| 105 | + print("InsertMany Error:", err) |
| 106 | + else |
| 107 | + print("Inserted documents:", json.encode(result.insertedIds)) |
| 108 | + end |
| 109 | +end) |
| 110 | + |
| 111 | + |
| 112 | +Mongo:UpdateOne({ |
| 113 | + collection = "users", |
| 114 | + query = { username = "jane_doe" }, |
| 115 | + update = { |
| 116 | + ["$set"] = { last_login = os.date("!%Y-%m-%dT%H:%M:%SZ") } or last_login = os.date("!%Y-%m-%dT%H:%M:%SZ") |
| 117 | + } |
| 118 | +}, function(err, result) |
| 119 | + if err then |
| 120 | + print("UpdateOne Error:", err) |
| 121 | + else |
| 122 | + print("Modified count:", result.modifiedCount) |
| 123 | + end |
| 124 | +end) |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +Mongo:UpdateMany({ |
| 129 | + collection = "users", |
| 130 | + query = { active = false }, |
| 131 | + update = { |
| 132 | + ["$set"] = { active = true } |
| 133 | + } |
| 134 | +}, function(err, result) |
| 135 | + if err then |
| 136 | + print("UpdateMany Error:", err) |
| 137 | + else |
| 138 | + print("Documents updated:", result.modifiedCount) |
| 139 | + end |
| 140 | +end) |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +Mongo:DeleteOne({ |
| 146 | + collection = "users", |
| 147 | + query = { username = "user_to_delete" } |
| 148 | +}, function(err, result) |
| 149 | + if err then |
| 150 | + print("DeleteOne Error:", err) |
| 151 | + else |
| 152 | + print("Deleted count:", result.deletedCount) |
| 153 | + end |
| 154 | +end) |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +Mongo:DeleteMany({ |
| 160 | + collection = "users", |
| 161 | + query = { inactive = true } |
| 162 | +}, function(err, result) |
| 163 | + if err then |
| 164 | + print("DeleteMany Error:", err) |
| 165 | + else |
| 166 | + print("Deleted documents:", result.deletedCount) |
| 167 | + end |
| 168 | +end) |
| 169 | + |
| 170 | + |
| 171 | +Mongo:Count({ |
| 172 | + collection = "users", |
| 173 | + query = { active = true } |
| 174 | +}, function(err, count) |
| 175 | + if err then |
| 176 | + print("CountDocuments Error:", err) |
| 177 | + else |
| 178 | + print("Active users count:", count) |
| 179 | + end |
| 180 | +end) |
| 181 | + |
| 182 | + |
| 183 | +Mongo:FindSpec({ |
| 184 | + collection = "users", |
| 185 | + query = { age = { ["$gt"] = 18 } }, |
| 186 | + limit = 50 |
| 187 | +}, function(err, data) |
| 188 | + if err then |
| 189 | + print("Error fetching users:", err) |
| 190 | + return |
| 191 | + end |
| 192 | + |
| 193 | + for _, user in ipairs(data) do |
| 194 | + print(("User: %s | Identifier: %s"):format(user.username, user.identifier)) |
| 195 | + end |
| 196 | +end) |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +-- TODO! |
| 201 | +Mongo:Aggregate({ |
| 202 | + collection = "users", |
| 203 | + pipeline = { |
| 204 | + { ["$match"] = { active = true } }, |
| 205 | + { ["$group"] = { |
| 206 | + _id = "$country", |
| 207 | + count = { ["$sum"] = 1 } |
| 208 | + }} |
| 209 | + } |
| 210 | +}, function(err, results) |
| 211 | + if err then |
| 212 | + print("Aggregate Error:", err) |
| 213 | + else |
| 214 | + for _, row in ipairs(results) do |
| 215 | + print(("Country: %s | Users: %d"):format(row._id, row.count)) |
| 216 | + end |
| 217 | + end |
| 218 | +end) |
| 219 | + |
| 220 | + |
| 221 | + |
87 | 222 | ``` |
88 | 223 |
|
89 | 224 |
|
|
0 commit comments