Crud Operations

CRUD Operations

Create, Read, Update, and Delete operations in gmsv_mongo

CRUD (Create, Read, Update, Delete) operations are the foundation of database interactions. Learn how to work with your data in MongoDB.

Overview

gmsv_mongo provides comprehensive CRUD methods for both single and bulk operations:

OperationSingleBulkDescription
CreateInsertOneInsertManyAdd new documents
ReadFindOne, Find-Retrieve documents
UpdateUpdateOneUpdateManyModify documents
DeleteDeleteOneDeleteManyRemove documents

Every CRUD operation has an async version (e.g., InsertOneAsync(), FindOneAsync()) that accepts callbacks. Use async in production to prevent server lag!

→ Learn About Async Operations

Quick Reference

The examples below use synchronous operations for simplicity. In production servers, use the async versions with callbacks!

Create Operations

-- 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 }
})

Read Operations

-- 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 Operations

-- 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 Operations

-- Delete one document
local deleted = collection:DeleteOne({ name = "Player1" })

-- Delete many documents
local deleted = collection:DeleteMany({ active = false })

Learn More

Explore each operation in detail:

Create Operations

Insert single and multiple documents

Read Operations

Find and query documents

Update Operations

Modify existing documents

Delete Operations

Remove documents from collections

Query Filters

Advanced query operators

Common Patterns

Best practices and patterns