Skip to content

68wooley/mongodb_densify_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB $densify Example

A Node.js project demonstrating MongoDB's $densify aggregation operator for filling gaps in time-series workout data. This project showcases how to use $densify to create continuous date ranges with zero-filled values for missing periods.

It was created as an example solution to a thread in Reddit

Features

  • MongoDB aggregation pipeline using $densify to fill date gaps
  • Timezone-aware date handling using date-fns-tz
  • Dynamic period grouping (daily, monthly, yearly) based on date range
  • Automatic label generation for different time periods
  • Workout session aggregation and analysis

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (v6.0 or higher - $densify requires MongoDB 6.0+)
  • MongoDB running locally or accessible connection string

Installation

  1. Clone the repository:
git clone <repository-url>
cd mongodb_densify_example
  1. Install dependencies:
npm install

Configuration

The project connects to MongoDB using the connection string defined in densify.js. By default, it uses:

  • Connection URI: mongodb://localhost:27017/fitness
  • Database: Fitness
  • Collections: users and workouts

To modify the connection, update the MONGODB_URI constant in densify.js:

const MONGODB_URI = 'mongodb://localhost:27017/fitness';

Usage

Run the script to execute the aggregation pipeline:

node densify.js

The script will:

  1. Connect to MongoDB
  2. Execute the testWorkouts() aggregation pipeline
  3. Display the results showing workout sessions grouped by time period
  4. Close the MongoDB connection

How It Works

The aggregation pipeline performs the following operations:

  1. Match: Filters users by a specific user ID
  2. Lookup: Joins workout data from the workouts collection
  3. Unwind: Expands the workout array
  4. Group: Groups workouts by day using $dateTrunc
  5. Densify: Fills in missing dates in the range (this is the key feature)
  6. Fill: Sets totalSessions to 0 for missing dates
  7. Project: Formats output with appropriate labels based on date range
  8. Group: Re-groups by period key (day/month/year depending on range)
  9. Sort: Orders results chronologically

Date Range Behavior

The pipeline adapts its output format based on the number of days:

  • ≤ 7 days: Daily view with day names (Mon, Tue, etc.)
  • ≤ 90 days: Daily view with date format (YYYY-MM-DD)
  • ≤ 365 days: Monthly view (Jan 2024, Feb 2024, etc.)
  • > 365 days: Yearly view (2024, 2025, etc.)

Dependencies

  • mongoose: ^8.0.0 - MongoDB object modeling
  • date-fns: ^3.0.0 - Date utility functions
  • date-fns-tz: ^3.2.0 - Timezone support for date-fns

Project Structure

mongodb_densify_example/
├── densify.js          # Main script with aggregation pipeline
├── package.json       # Project dependencies
├── package-lock.json  # Dependency lock file
└── README.md         # This file

Notes

  • The $densify operator requires MongoDB 6.0 or higher
  • The current implementation uses a hardcoded user ID for testing
  • The timezone is set to 'Asia/Kolkata' but can be modified in the testWorkouts() function
  • The default date range is 7 days but can be adjusted via the noOfDays variable

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

About

Sample aggregation code using densify in response to :

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors