DocumentationFauxDB Documentation
FauxDB Examples
MongoDB Protocol Examples
Practical examples using MongoDB wire protocol with FauxDB.
Connect with mongosh
Connection examples
# Connect to FauxDB
mongosh mongodb://localhost:27017/mydb
# With authentication
mongosh "mongodb://username:password@localhost:27017/mydb?authSource=admin"CRUD Operations
MongoDB CRUD
// Insert documents
db.users.insertOne({
name: "John Doe",
email: "john@example.com",
age: 30,
created: new Date()
})
db.users.insertMany([
{ name: "Jane Smith", email: "jane@example.com", age: 25 },
{ name: "Bob Wilson", email: "bob@example.com", age: 35 }
])
// Find documents
db.users.find({ age: { $gte: 25 } })
db.users.findOne({ email: "john@example.com" })
// Update documents
db.users.updateOne(
{ email: "john@example.com" },
{ $set: { age: 31, updated: new Date() } }
)
db.users.updateMany(
{ age: { $lt: 30 } },
{ $inc: { age: 1 } }
)
// Delete documents
db.users.deleteOne({ email: "john@example.com" })
db.users.deleteMany({ age: { $gte: 40 } })Aggregation Pipeline
Aggregation examples
// Group by age and count
db.users.aggregate([
{ $group: { _id: "$age", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
])
// Complex aggregation
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: {
_id: "$customer_id",
total: { $sum: "$amount" },
count: { $sum: 1 },
avg: { $avg: "$amount" }
}
},
{ $sort: { total: -1 } },
{ $limit: 10 }
])Indexes
Index management
// Create indexes
db.users.createIndex({ email: 1 }, { unique: true })
db.users.createIndex({ name: 1, age: -1 })
db.users.createIndex({ location: "2dsphere" })
// List indexes
db.users.getIndexes()
// Drop index
db.users.dropIndex("email_1")MySQL Protocol Examples
Practical examples using MySQL wire protocol with FauxDB.
Connect with MySQL Client
Connection examples
# Connect to FauxDB
mysql -h localhost -P 3306 -u username -p mydb
# Connection string
mysql://username:password@localhost:3306/mydbSQL Operations
SQL CRUD operations
-- Create table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255) UNIQUE,
age INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert data
INSERT INTO users (name, email, age)
VALUES ('John Doe', 'john@example.com', 30);
INSERT INTO users (name, email, age) VALUES
('Jane Smith', 'jane@example.com', 25),
('Bob Wilson', 'bob@example.com', 35);
-- Query data
SELECT * FROM users WHERE age >= 25;
SELECT name, email FROM users ORDER BY age DESC;
-- Update data
UPDATE users SET age = 31 WHERE email = 'john@example.com';
-- Delete data
DELETE FROM users WHERE age > 40;Joins & Subqueries
Advanced SQL queries
-- Join tables
SELECT u.name, o.order_id, o.amount
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE o.status = 'completed';
-- Subquery
SELECT name, email
FROM users
WHERE id IN (
SELECT user_id FROM orders
WHERE amount > 100
);
-- Aggregation
SELECT
u.name,
COUNT(o.id) as order_count,
SUM(o.amount) as total_spent
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.id, u.name
HAVING total_spent > 1000;Application Integration
Code examples for integrating FauxDB with popular programming languages.
Python - MongoDB Driver
Python pymongo example
from pymongo import MongoClient
# Connect to FauxDB
client = MongoClient('mongodb://localhost:27017/')
db = client['mydb']
collection = db['users']
# Insert
collection.insert_one({
'name': 'John',
'email': 'john@example.com'
})
# Query
users = collection.find({'age': {'$gte': 25}})
for user in users:
print(user)Python - MySQL Driver
Python mysql-connector example
import mysql.connector
# Connect to FauxDB
conn = mysql.connector.connect(
host='localhost',
port=3306,
user='username',
password='password',
database='mydb'
)
cursor = conn.cursor()
# Query
cursor.execute(
"SELECT * FROM users WHERE age >= %s",
(25,)
)
for row in cursor.fetchall():
print(row)
conn.close()Node.js - MongoDB Driver
Node.js mongodb example
const { MongoClient } = require('mongodb');
const client = new MongoClient(
'mongodb://localhost:27017'
);
await client.connect();
const db = client.db('mydb');
const collection = db.collection('users');
// Insert
await collection.insertOne({
name: 'John',
email: 'john@example.com'
});
// Query
const users = await collection
.find({ age: { $gte: 25 } })
.toArray();Node.js - MySQL Driver
Node.js mysql2 example
const mysql = require('mysql2/promise');
const connection = await mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'username',
password: 'password',
database: 'mydb'
});
// Query
const [rows] = await connection.execute(
'SELECT * FROM users WHERE age >= ?',
[25]
);
console.log(rows);