MongoDB
Port 27017 Flexible JSON document storage.
Connection
MONGODB_URI=mongodb://myapp_user:PASSWORD@db.bizfylabs.com:27017/myapp_db?authSource=myapp_db
Setup
npm install mongodb # Node.js
pip install pymongo # Python
import { MongoClient } from 'mongodb';
const client = new MongoClient(process.env.MONGODB_URI);
await client.connect();
const db = client.db('myapp_db');
Create Collection (with validation)
await db.createCollection('orders', {
validator: {
$jsonSchema: {
bsonType: 'object',
required: ['userId', 'total', 'status'],
properties: {
userId: { bsonType: 'string' },
total: { bsonType: 'number', minimum: 0 },
status: { enum: ['pending', 'paid', 'shipped'] },
},
},
},
});
// Or auto-created on first insert
const users = db.collection('users');
CRUD — Node.js
Create
// Insert one
const result = await users.insertOne({
email: 'alice@example.com',
name: 'Alice',
status: 'active',
createdAt: new Date(),
});
// Insert many
await users.insertMany([
{ email: 'bob@example.com', name: 'Bob' },
{ email: 'carol@example.com', name: 'Carol' },
]);
Read
// Find one
const user = await users.findOne({ email: 'alice@example.com' });
const byId = await users.findOne({ _id: new ObjectId(id) });
// Find many + filter + sort + pagination
const list = await users
.find({ status: 'active' })
.sort({ createdAt: -1 })
.skip(0)
.limit(20)
.toArray();
// Projection (select fields)
await users.find({}, { projection: { email: 1, name: 1, _id: 0 } }).toArray();
// Count
const total = await users.countDocuments({ status: 'active' });
// Aggregation
const stats = await db.collection('orders').aggregate([
{ $match: { status: 'paid' } },
{ $group: { _id: '$userId', total: { $sum: '$amount' }, count: { $sum: 1 } } },
{ $sort: { total: -1 } },
{ $limit: 10 },
]).toArray();
Update
// Update one
await users.updateOne(
{ _id: userId },
{ $set: { name: 'Alice Updated', updatedAt: new Date() } }
);
// Update many
await users.updateMany(
{ status: 'pending' },
{ $set: { status: 'active' } }
);
// Upsert
await users.updateOne(
{ email: 'new@example.com' },
{ $set: { name: 'New User' }, $setOnInsert: { createdAt: new Date() } },
{ upsert: true }
);
// Increment
await db.collection('counters').updateOne(
{ _id: 'orders' },
{ $inc: { seq: 1 } },
{ upsert: true }
);
Delete
await users.deleteOne({ _id: userId });
await users.deleteMany({ status: 'archived' });
// Drop collection (destructive)
await db.collection('temp_logs').drop();
CRUD — Python
users = db["users"]
# Create
users.insert_one({"email": "a@b.com", "name": "Alice"})
users.insert_many([{"email": "b@c.com"}, {"email": "d@e.com"}])
# Read
user = users.find_one({"email": "a@b.com"})
list(users.find({"status": "active"}).limit(20))
# Update
users.update_one({"_id": uid}, {"$set": {"name": "Bob"}})
users.update_many({"status": "pending"}, {"$set": {"status": "active"}})
# Delete
users.delete_one({"_id": uid})
users.delete_many({"status": "archived"})
Indexes
await users.createIndex({ email: 1 }, { unique: true });
await orders.createIndex({ userId: 1, createdAt: -1 });
await articles.createIndex({ title: 'text', body: 'text' });
await sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 }); // TTL
Admin — Create Database & User
Platform admin only.
docker exec -it bizfy-mongodb mongosh -u admin -p --authenticationDatabase admin
// Create read/write user for one database
use myapp_db
db.createUser({
user: "myapp_user",
pwd: "strong-random-password",
roles: [{ role: "readWrite", db: "myapp_db" }]
})
Read-only User
db.createUser({
user: "myapp_readonly",
pwd: "strong-random-password",
roles: [{ role: "read", db: "myapp_db" }]
})
List / Delete User / Database
use myapp_db
db.getUsers() // list users
db.dropUser("myapp_user") // delete user
db.dropDatabase() // delete entire database (destructive)
// List all databases
db.adminCommand('listDatabases')
Send to Developer
MONGODB_URI=mongodb://myapp_user:PASSWORD@db.bizfylabs.com:27017/myapp_db?authSource=myapp_db