- Database:
userdb - Collection:
users - Records: 10 users with mock PII data
Each user document contains:
- First Name & Last Name
- Email Address
- Phone Number
- Social Security Number (SSN)
- Date of Birth
- Full Address (street, city, state, zip, country)
- Timestamps (createdAt, updatedAt)
- setup_database.js - Creates the database with mock PII data
- anonymize_data.js - Simple anonymization (User 1, User 2, etc.)
- anonymize_with_hash.js - Hash-based anonymization (non-reversible)
- restore_original_data.js - Restores original mock PII data
# Connect to MongoDB and run the setup script
mongosh < setup_database.jsOr run it directly in mongosh:
# Connect to MongoDB
mongosh
# Load and execute the script
load("setup_database.js")Option A: Simple Anonymization
# Anonymizes to User 1, User 2, etc.
mongosh < anonymize_data.jsOption B: Hash-Based Anonymization
# Uses one-way hash functions for consistent anonymization
mongosh < anonymize_with_hash.js# Restores the original mock PII data
mongosh < restore_original_data.js// Connect to the database
use userdb;
// Count documents
db.users.countDocuments();
## Anonymization Details
### Simple Anonymization (anonymize_data.js)
- Names → "User [ID]"
- Email → "user[ID]@anonymized.local"
- Phone → "+1-XXX-XXX-[ID]"
- SSN → "XXX-XX-[ID]"
- Date of Birth → Year only (YYYY-01-01)
- Address → Redacted values
### Hash-Based Anonymization (anonymize_with_hash.js)
- Uses one-way hash functions
- Creates consistent but non-reversible values
- Original data cannot be recovered
- Maintains referential consistency
- Stores hash of original email for tracking
### Comparison
| Method | Pros | Cons | Use Case |
|--------|------|------|----------|
| Simple | Easy to understand, predictable | Pattern visible | Testing, demos |
| Hash-Based | More realistic, non-reversible | Less predictable | Production-like testing |// View all users db.users.find().pretty();
// Find a specific user by email db.users.findOne({ email: "john.smith@email.com" });
// View indexes db.users.getIndexes();
## Sample Queries
```javascript
// Find users by city
db.users.find({ "address.city": "New York" });
// Find users born after 1990
db.users.find({ dateOfBirth: { $gte: new Date("1990-01-01") } });
// Update a user's phone number
db.users.updateOne(
{ email: "john.smith@email.com" },
{ $set: { phone: "+1-555-9999", updatedAt: new Date() } }
);
// Delete a user
db.users.deleteOne({ email: "john.smith@email.com" });