Query Language Snippets
All Snippets
Redis
Combined
Use LPUSH and LRANGE for list operations.
LPUSH mylist "item1"
LRANGE mylist 0 -1
Rust
Combined
Redis basic operations.
use redis::{Commands, RedisResult};
fn main() -> RedisResult {
let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_connection()?;
con.set("foo", "bar")?;
let v: String = con.get("foo")?;
println!("foo = {}", v);
con.del("foo")?;
Ok(())
}
MongoDB
Combined
Aggregate documents with $group, $sort, $match, etc.
db.orders.aggregate([
{ $match: { status: "complete" } },
{ $group: { _id: "$customerId", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } },
{ $limit: 5 }
]);
SQL
Combined
Use a transaction (BEGIN, COMMIT, ROLLBACK).
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
-- In case of error:
ROLLBACK;
Redis
Combined
Use ZADD and ZRANGE for sorted set operations.
ZADD mysortedset 1 "member1"
ZRANGE mysortedset 0 -1 WITHSCORES
Cypher
Combined
Use MERGE to create or match a node.
MERGE (n:Person {name: 'Alice'})
RETURN n;
Python
Combined
MongoDB basic CRUD operations.
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
db = client.example
db.users.insert_one({"name": "Alice", "age": 30})
docs = db.users.find({"age": {"$gt": 25}})
for doc in docs:
print(doc)
client.close()
Python
Combined
Redis basic operations.
import redis
r = redis.Redis(host='localhost', port=6379)
r.set('foo', 'bar')
print(r.get('foo'))
r.delete('foo')
C# Entity Framework
Combined
Entity Framework basic CRUD operations.
using (var context = new MyDbContext())
{
// Create
var user = new User { Name = "Alice", Age = 30 };
context.Users.Add(user);
context.SaveChanges();
// Read
var users = context.Users.Where(u => u.Age > 25).ToList();
// Update
var alice = context.Users.First(u => u.Name == "Alice");
alice.Age = 31;
context.SaveChanges();
// Delete
context.Users.Remove(alice);
context.SaveChanges();
}
SQL
Combined
Combine results with UNION (removes duplicates).
SELECT name FROM table1 UNION SELECT name FROM table2;
SQL
Combined
Combine results with UNION ALL (includes duplicates).
SELECT name FROM table1 UNION ALL SELECT name FROM table2;
SQLModel
Combined
Use SQLModel for transactions.
with Session(engine) as session:
try:
user = User(name='Alice', age=30)
session.add(user)
session.commit()
except:
session.rollback()
raise