CRUD (Create, Read, Update, Delete) operations are the foundation of database interactions. Learn how to work with your data in MongoDB.
gmsv_mongo provides comprehensive CRUD methods for both single and bulk operations:
| Operation | Single | Bulk | Description |
|---|---|---|---|
| Create | InsertOne | InsertMany | Add new documents |
| Read | FindOne, Find | - | Retrieve documents |
| Update | UpdateOne | UpdateMany | Modify documents |
| Delete | DeleteOne | DeleteMany | Remove documents |
Every CRUD operation has an async version (e.g., InsertOneAsync(), FindOneAsync()) that accepts callbacks. Use async in production to prevent server lag!
The examples below use synchronous operations for simplicity. In production servers, use the async versions with callbacks!
-- Insert one document
local id = collection:InsertOne({ name = "Player1", level = 5 })
-- Insert many documents
local ids = collection:InsertMany({
{ name = "Player1", level = 5 },
{ name = "Player2", level = 10 }
})
-- Find multiple documents
local results = collection:Find({ level = { ["$gte"] = 5 } }, 10)
-- Find one document
local doc = collection:FindOne({ name = "Player1" })
-- Count documents
local count = collection:Count({ active = true })
-- Update one document
local updated = collection:UpdateOne(
{ name = "Player1" },
{ ["$set"] = { level = 10 } }
)
-- Update many documents
local updated = collection:UpdateMany(
{ active = true },
{ ["$inc"] = { credits = 100 } }
)
-- Delete one document
local deleted = collection:DeleteOne({ name = "Player1" })
-- Delete many documents
local deleted = collection:DeleteMany({ active = false })
Explore each operation in detail: