Educational demos showcasing MongoDB integration with JavaScript using @enciv/mongo-collections.
- Node.js: Version 22.21.1 (specified in
.nvmrcfile - automatically loaded in compatible environments) - MongoDB: Either:
- Local MongoDB installation, OR
- MongoDB Atlas account (free tier available at mongodb.com)
npm installThis will install:
@enciv/mongo-collections- Simplified MongoDB collections wrapperdotenv- Environment variable management
Create a .env file in the project root (use .env.example as a template):
For local MongoDB:
MONGO_DB_URI=mongodb://localhost:27017/collections-demo
For MongoDB Atlas:
MONGO_DB_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/collections-demo
Replace <username>, <password>, and <cluster> with your actual MongoDB Atlas credentials.
git init
git add .
git commit -m "Initial commit: collections-demo project"File: demo1-form-collection.js
This demo demonstrates:
- Connecting to MongoDB
- Collecting user input via console
- Saving data to a collection
- Querying all entries
- Extracting timestamps from MongoDB ObjectIds
Run the demo:
node demo1-form-collection.jsOr use the npm script:
npm run demo1Example output:
Welcome to the Form Collection Demo!
First Name: John
Favorite Fruit: apples
Entry saved!
All entries in the collection:
1. firstName: John, favoriteFruit: apples, created: 2026-01-13T10:30:45.123Z
2. firstName: Jane, favoriteFruit: oranges, created: 2026-01-13T10:32:18.456Z
Note: The 'created' timestamp is extracted from the MongoDB ObjectId
How it works:
- Prompts for first name and favorite fruit via stdin
- Saves each entry to the
formscollection - MongoDB automatically generates an
_id(ObjectId) with embedded timestamp - Displays all entries with timestamps extracted from their ObjectIds
- Data persists between runs
File: demo2-form-with-schema.js
This demo demonstrates:
- Defining a collection with MongoDB schema validation
- Using
collectionOptionsto enforce data rules - Handling validation errors
- Ensuring data integrity at the database level
Run the demo:
node demo2-form-with-schema.jsOr use the npm script:
npm run demo2What's different from Demo 1:
- Uses a separate collection (
forms-validated) with schema validation - MongoDB enforces that
firstNameandfavoriteFruitmust be strings and are required - Invalid data will be rejected before insertion
- Shows how to handle validation errors (error code 121)
Key code difference:
class Forms extends Collection {
static collectionName = 'forms-validated'
static collectionOptions = {
validator: {
$jsonSchema: {
bsonType: 'object',
required: ['firstName', 'favoriteFruit'],
properties: {
firstName: { bsonType: 'string' },
favoriteFruit: { bsonType: 'string' }
}
}
}
}
}File: demo3-aggregation-lookup.js
This demo demonstrates:
- Storing related documents with parent references
- Using MongoDB aggregation pipeline
- Using
$lookupto join related documents - Retrieving all related data in a single round trip to the database
Run the demo:
node demo3-aggregation-lookup.jsOr use the npm script:
npm run demo3Example interaction:
Welcome to the Aggregation Demo!
First Name: Alice
Favorite Fruit: bananas
Entry saved!
Enter friends (press Enter without typing to finish):
Friend 1: Bob
Friend 2: Charlie
Friend 3: Diana
Friend 4:
3 friend(s) added!
All entries in the collection (with friends joined):
1. firstName: Alice, favoriteFruit: bananas, friends: [Bob, Charlie, Diana], created: 2026-01-13T10:45:23.456Z
Note: All data retrieved in a single aggregation query using $lookup
Note: Each friend is stored as a separate document with a parentId reference
How it works:
- Creates a main entry with
firstNameandfavoriteFruit - Prompts for friends repeatedly until user hits Enter
- Each friend is stored as a separate document with
friendNameandparentId - Uses aggregation pipeline with
$lookupto join friends with their parent in one query - Demonstrates efficient data retrieval - all related data in a single round trip
Key aggregation pipeline:
Forms.aggregate([
{ $match: { firstName: { $exists: true } } },
{
$lookup: {
from: 'forms-aggregate',
localField: '_id',
foreignField: 'parentId',
as: 'friendDocs'
}
},
{ $project: { friends: '$friendDocs.friendName' } }
])File: demo4-separate-collections.js
This demo demonstrates:
- Defining multiple collections with their own schema validation
- Storing related data in separate collections (Forms and Friends)
- Using
$lookupto join data across different collections - Schema validation on both collections
- Best practice: separating concerns into different collections
Run the demo:
node demo4-separate-collections.jsOr use the npm script:
npm run demo4What's different from Demo 3:
- Friends stored in a separate
Friendscollection (not mixed with Forms) - Both collections have schema validation
- Forms schema requires
firstNameandfavoriteFruitas strings - Friends schema requires
friendName(string) andparentId(ObjectId) - Shows proper data modeling with separate collections
Key collection definitions:
class Forms extends Collection {
static collectionName = 'forms-with-friends'
static collectionOptions = {
validator: { $jsonSchema: { /* ... */ } }
}
}
class Friends extends Collection {
static collectionName = 'friends'
static collectionOptions = {
validator: {
$jsonSchema: {
required: ['friendName', 'parentId'],
properties: {
friendName: { bsonType: 'string' },
parentId: { bsonType: 'objectId' }
}
}
}
}
}Aggregation across collections:
Forms.aggregate([
{
$lookup: {
from: 'friends', // Join with Friends collection
localField: '_id',
foreignField: 'parentId',
as: 'friendDocs'
}
}
])MongoDB's ObjectId contains a timestamp of when the document was created. This demo shows how to extract it using _id.getTimestamp(), eliminating the need for separate timestamp fields.
The library provides a simple connection pattern:
const { Mongo } = require('@enciv/mongo-collections');
await Mongo.connect(process.env.MONGO_DB_URI);
const collection = Mongo.db.collection('collectionName');This creates a single connection that can be used throughout your project.
"Cannot connect to MongoDB"
- Verify your
MONGO_DB_URIin the.envfile - For local MongoDB, ensure the MongoDB service is running
- For Atlas, check your network access settings and credentials
"Module not found"
- Run
npm installto install dependencies
Node.js version issues
- Check you're using Node.js 22.21.1 (see
.nvmrc) - If using nvm:
nvm use
Additional demos will cover:
- Query and filter operations
- Updating existing entries
- Delete operations
- Indexing and performance
- Aggregation pipelines
MIT