Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 2, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 2, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 2, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 2, 2025

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025

      I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

      June 2, 2025

      This new official Xbox 4TB storage card costs almost as much as the Xbox SeriesXitself

      June 2, 2025

      I may have found the ultimate monitor for conferencing and productivity, but it has a few weaknesses

      June 2, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      May report 2025

      June 2, 2025
      Recent

      May report 2025

      June 2, 2025

      Write more reliable JavaScript with optional chaining

      June 2, 2025

      Deploying a Scalable Next.js App on Vercel – A Step-by-Step Guide

      June 2, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025
      Recent

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025

      I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

      June 2, 2025

      This new official Xbox 4TB storage card costs almost as much as the Xbox SeriesXitself

      June 2, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Databases»MongoDB Atlas Stream Processing Now Supports Session Windows!

    MongoDB Atlas Stream Processing Now Supports Session Windows!

    May 29, 2025

    We’re excited to announce that MongoDB Atlas Stream Processing now supports Session Windows!

    This powerful feature lets you build streaming pipelines that analyze and process related events that occur together over time, grouping them into meaningful sessions based on periods of activity. For instance, you can now track all of a customer’s interactions during a shopping journey, treating it as a single session that ends when they’re inactive for a specified period of time.

    Whether you’re analyzing user behavior, monitoring IoT device activities, or tracking system operations, Atlas Stream Processing’s Session Windows make it easy to transform your continuous data streams into actionable insight, and make the data available wherever you need to use it.

    What are Session Windows?

    Session Windows are a powerful way to analyze naturally occurring activity patterns in your data by grouping related events that happen close together in time. Think of how users interact with websites or apps—they tend to be active for a period, then take breaks, then return for another burst of activity. Session Windows automatically detect these patterns by identifying gaps in activity, allowing you to perform aggregations and transformations on these meaningful periods of activity.

    As an example, imagine you’re an e-commerce company looking to better understand what your customers do during each browsing session to help improve conversions.

    With Atlas Stream Processing, you can build a pipeline that:

    1. Collects all the product pages a user visits during their browsing session

    2. Records the name, category, and price of each item viewed, plus whether items were added to a cart

    3. Automatically considers a session complete after 15 minutes of user inactivity

    4. Sends the session data to cloud storage to improve recommendation engines

    With this pipeline, you provide your recommendation engine with ready-to-use data about your user sessions to improve your recommendations in real time. Unlike fixed time-based windows (tumbling or hopping), Session Windows adapt dynamically to each user’s behavior patterns.

    How does it work?

    Session Windows work similarly to the hopping and tumbling windows Atlas Stream Processing already supports, but with a critical difference: while those windows open and close on fixed time intervals, Session Windows dynamically adjust based on activity patterns.

    To implement a Session Window, you specify three required components:

    • partitionBy: This is the field or fields that group your records into separate sessions. For instance, if tracking user sessions, use unique user IDs to ensure each user’s activity is processed separately.

    • gap: This is the period of inactivity that signals the end of a session. For instance, in the above example, we consider a user’s session complete when they go 15 minutes without clicking on a link in the website or app.

    • pipeline: These are the operations you want to perform on each session’s data. This may include counting the number of pages a user visited, recording the page they spent the most time on, or noting which pages were visited multiple times.

    You then add this Session Window stage to your streaming aggregation pipeline, and Atlas Stream Processing continuously processes your incoming data, groups events into sessions based on your configuration, and applies your specified transformations. The results flow to your designated output destinations in real-time, ready for analysis or to trigger automated actions.

    A quick example

    Let’s say you want to build the pipeline that we mentioned above to track user sessions, notify them if they have items in their cart but haven’t checked out, and move their data downstream for analytics. You might do something like this:

    1. Configure your source and sink stages

    This is where you define the connections to any MongoDB or external location you intend to receive data from (source) or send data to (sink).

    // Set your source to be change streams from the pageViews, cartItems, and orderedItems collections
let sourceCollections = {
    $source: {
        connectionName: "ecommerce",
        "db": "customerActivity",
        "coll": ["pageViews", "cartItems", "orderedItems"]
     }
}


// Set your destination (sink) to be the userSessions topic your recommendation engine consumes data from
let emitToRecommendationEngine = {
    $emit: {
        connectionName: "recommendationEngine",
        topic: "userSessions",
    }
};

// Create a connection to your sendCheckoutReminder Lambda function that sends a reminder to users to check out if they have items in their cart when the session ends
let sendReminderIfNeeded = {
    $externalFunction: {
        "connectionName": "operations",
        "as": "sendCheckoutReminder",
        "functionName": "arn:aws:lambda:us-east-1:123412341234:function:sendCheckoutReminder"
    }
 }


    2. Define your Session Window logic

    This is where you specify how data will be transformed in your stream processing pipeline.

    // Step 1.  Create a stage that pulls only the fields you care about from the change logs.
// Every document will have a userId and itemId as all collections share that field.  Fields not present will be null.
let extractRelevantFields = {
   $project: {
       userId: "$fullDocument.userId",
       itemId: "$fullDocument.itemId",
       category: "$fullDocument.category",
       cost: "$fullDocument.cost",
       viewedAt: "$fullDocument.viewedAt",
       addedToCartAt: "$fullDocument.addedToCartAt",
       purchasedAt: "$fullDocument.purchasedAt"
   }
};

// Step 2.  By setting _id to $userId this group all the documents by the userId.  Fields not present in any records will be null.
let groupSessionData = {
   $group: {
       _id: "$userId",
       itemIds: { $addToSet: "$itemId" },
       categories: { $addToSet: "$category" },
       costs: { $addToSet: "$cost" },
       viewedAt: { $addToSet: "$viewedAt" },
       addedToCartAt: { $addToSet: "$addedToCartAt" },
       purchasedAt: { $addToSet: "$purchasedAt" }
   }
};

// Step 3.  Create a session window that closes after 15 minutes of inactivity.  The pipeline specifies all operations to be performed on documents sharing the same userId within the window.
let createSession = { $sessionWindow: {
   partitionBy: "$_id",
   gap: { unit: "minute", size: 15},
   pipeline: [
       groupSessionData
   ]
}};


    3. Create and start your stream processor

    The last step is simple: create and start your stream processor.

    //  Create your pipeline array.  The session data will be sent to the external function defined in sendReminderIfNeeded, and then it will be emitted to the recommendation engine Kafka topic.
finalPipeline = [
   sourceCollections,
   extractRelevantFields,
   createSession,
   sendReminderIfNeeded,
   emitToUserSessionTopic
];

// Create your stream processor
sp.createStreamProcessor("userSessions", finalPipeline)

// Start your stream processor
sp.userSessions.start()


    And that’s it! Your stream processor now runs continuously in the background with no additional management required. As users navigate your e-commerce website, add items to their carts, and make purchases, Atlas Stream Processing automatically:

    • Tracks each user’s activity in real-time

    • Groups events into meaningful sessions based on natural usage patterns

    • Closes sessions after your specified period of inactivity (15 minutes)

    • Triggers reminders for users with abandoned carts

    • Delivers comprehensive session data to your analytics systems

    All of this happens automatically at scale without requiring ongoing maintenance or manual intervention. Session Windows provide powerful, activity-based data processing that adapts to users’ behavioral patterns rather than forcing their actions into arbitrary time buckets.

    Ready to get started? Log in or sign up for Atlas today to create stream processors. You can learn more about Session Windows or get started using our tutorial.

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleInclusivity with Voice & Language [SUBSCRIBER]
    Next Article Ecosystem Partnerships: Driving Mainframe Innovation and Future-Ready Solutions

    Related Posts

    Security

    ⚡ Weekly Recap: APT Intrusions, AI Malware, Zero-Click Exploits, Browser Hijacks and More

    June 2, 2025
    Security

    Exploitation Risk Grows for Critical Cisco Bug

    June 2, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-45427 – Tenda AC9 Stack Overflow Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2022-44759 – HCL Leap SVG Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Teams Rooms on Windows will support cross-platform meetings via SIP

    Development

    April 4, 2025: AI updates from the past week — Claude for Education, new website for exploring Amazon Nova models, and Solo.io’s MCP Gateway

    Tech & Work

    Highlights

    Development

    Step by step guide to secure JDBC SSL connection with Postgres in AWS Glue

    April 6, 2024

    Have you ever tried connecting a database to AWS Glue using a JDBC SSL encryption…

    CVE-2025-3828 – PHPGurukul Men Salon Management System SQL Injection Vulnerability

    April 20, 2025

    Researchers from UNC-Chapel Hill Introduce CTRL-Adapter: An Efficient and Versatile AI Framework for Adapting Diverse Controls to Any Diffusion Model

    April 17, 2024

    Amazon Bedrock Prompt Management is now available in GA

    November 7, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.