ToollessToolless

Performance

Optimize Toolless for better performance

Scalability Limits

Collection SizeReadWrite
< 1K docsExcellentExcellent
1K - 10KExcellentGood
10K - 100KGoodFair
> 100KConsider migrationConsider migration

Query Optimization

Use Indexes

// Before: full collection scan
await users.find({ email: "alice@example.com" }).toArray();

// After: indexed lookup
await users.createIndex({ email: 1 });
await users.find({ email: "alice@example.com" }).toArray();

Limit Results

// Bad: loads entire collection
const all = await users.find({}).toArray();

// Good: paginated
const page = await users.find({}).limit(20).skip(0).toArray();

Project Only Needed Fields

// Bad: returns full documents
const users = await users.find({}).toArray();

// Good: returns only needed fields
const names = await users.find({}).project({ name: 1, email: 1 }).toArray();

Write Optimization

Batch Inserts

// Bad: multiple write operations
for (const user of users) {
  await collection.insertOne(user);
}

// Good: single write operation
await collection.insertMany(users);

Update Operators

// Bad: replaces entire document
await users.updateOne(
  { _id: "123" },
  { name: "Alice", email: "new@example.com", ...allOtherFields }
);

// Good: updates specific fields
await users.updateOne({ _id: "123" }, { $set: { email: "new@example.com" } });

Memory Management

Collections are loaded into memory. For large collections:

  1. Split data across multiple collections
  2. Archive old data
  3. Use pagination for queries
  4. Consider migration to MongoDB

Compaction

Over time, deleted documents leave gaps. Compact to reclaim space:

toollessdb compact mydb users

Auto-compaction triggers at 50% dead ratio.

Monitoring

Check collection stats:

toollessdb stats mydb users

Output:

Collection: users
Documents: 1,500
File size: 2.4 MB
Dead ratio: 15%

Best Practices

  1. Create indexes for frequently queried fields
  2. Use pagination for large result sets
  3. Project only needed fields
  4. Batch write operations
  5. Monitor collection sizes
  6. Compact when dead ratio is high
  7. Archive old data periodically

On this page