Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Beginners

    5 Critical Tips for Designing Your First Website

    LinkedIn

    Building Authority on LinkedIn: Strategies to Attract Prospects

    JavaScript

    Daily Schedule Post

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Programming / MongoDb. Capped collections
    Programming

    MongoDb. Capped collections

    Stepan ZharychevBy Stepan ZharychevAugust 7, 2017Updated:April 5, 2019No Comments3 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    MongoDb. Capped collections
    MongoDb. Capped collections
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    Today we’re going to discuss not really popular, but really useful thing in MongoDB – capped collections. What’re they, how and where to use them. Let’s start!

    The main thing that you need to know about capped collections at first that’s set of restrictions – they don’t have all the functionality of common collections:

    1. No delete operations;
    2. All elements in collection should have an equal size (update will be rejected if element has different size than it used to have for example);
    3. It works as queue and because of this no index is required;

    The large set of restrictions, some of them can be lethal for normal collection usage for obvious reasons, so then exactly can we use such restricted set then? Let’s discuss some specifics in mechanics before make a decision and come to the conclusion.

    Mechanic differences provide us following profits:

    1. More than 10 000 read/write operations per second (that’s really good performance for MongoDB);
    2. Cursor that can be used for tracking of new entry adding to collection;
    3. Auto-removing from collection according to FIFO rule than size exceeds given limitations;
    MongoDb. Capped collections. Profit
    MongoDb. Capped collections. Profit

    I believe now it comes to be more interesting for you, so let’s proof that this functionality is actually used, and the best example at this point is that mongo actually uses it by itself!
    Local.startup_log – collection that stores all the MongoDB settings on start is actually a capped one.

    Oplog – collection that stores logs about all your operation and sits behind everything that can be done in MongoDB.

    Read More:  Finding the Best Way to Learn JavaScript

    So MongoDB uses those collections by itself, that allows it be efficient in many points. I believe that after those examples we definitely need to try this thing by ourselves. But where? In my mind the basic points of usage for such collections can be caching and logging, because for both cases it’s fast and can clean memory by itself.

    So let’s do some coding and see how to use it with Node.js. First of all we need to initialize this collection and there’re differences with common collection already, you need to set capped flag to true and specify amount that collection can take from memory (as you remember it’s required for self cleaning and efficient operations upon the data).

    let mongodb = require('mongodb');
    let MongoClient = mongodb.MongoClient;
    
    MongoClient.connect('mongodb://localhost:27017/capped').then((db) => {
        db.createCollection('errorLogs', {
            capped: true,
            size: 1000000
        });
    });

    So as soon as we’re done with collection initialization let’s do something with this one. And here we’re going to initialize cursor as was discussed before. Cursor is a special entity that allows us to track adding operations to collection and can be useful for different things, for example, you tracked adding of fatal log error to logs, inside cursor can sit logic that will send email to responsible admin/programmer of this part of code.

    let mongodb = require('mongodb');
    let MongoClient = mongodb.MongoClient;
    
    MongoClient.connect('mongodb://localhost:27017/capped').then((db) => {
        const collection = db.collection('errorLogs');
    
        const cursor = collection.find({}, {tailable: true, awaitdata: true});
        const cursorStream = cursor.stream();
        cursorStream.on('data', function (data) {
            console.log('added', data);
        });
    
        collection.insert({
            type: 'error',
            time: new Date().toISOString(),
            message: 'Something bad happend!'
        });
    });

    Be free to play with this code and you will understand much better how the capped collections work. Let’s summarize now what’re the differences here:

    Read More:  Design Patterns Overview: Helping You Write Better Software
    MongoDb. Capped collections. Summarizing
    MongoDb. Capped collections. Summarizing

    Now I believe you know the main scope of usage for capped collection, how to initialize and work upon them. Be free to ask questions in comments and offer your best cases of usage. Good luck!

    We are looking forward to meeting you on our website blog.soshace.com

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Stepan Zharychev
    • Website

    Related Posts

    Mastering REST APIs: Essential Techniques for Programmers

    December 18, 2024

    Crafting Interactive User Interfaces Using JavaScript Techniques

    December 17, 2024

    Effective Strategies for Utilizing Frameworks in Web Development

    December 16, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Interview December 1, 2024

    Balancing Enthusiasm: Effective Interview Strategies

    Balancing enthusiasm during interviews is crucial for effective communication. Candidates should exhibit genuine interest while maintaining composure. Employing strategies such as active listening and targeted responses can enhance engagement without overwhelming interviewers.

    Overview of FREE Python Programming Courses for Beginners

    September 9, 2019

    Git – Bad Practices

    September 20, 2016

    How and When to Ask for Remote Work

    August 12, 2019

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Dockerization of Node.JS Applications on Amazon Elastic Containers

    Node.js February 7, 2020

    Integrating Data Privacy into Effective Software Development

    Development December 17, 2024

    Building React Components Using Children Props and Context API

    JavaScript August 22, 2019

    Create simple POS with React, Node and MongoDB #2: Auth state, Logout, Update Profile

    JavaScript January 22, 2020

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    Medical Marketing

    Enhancing Online Reputation Management in Hospitals: A Guide

    JavaScript

    Using SWR for Efficient Data Fetching in Next.js Applications

    Beginners

    TOP SQL & Database Courses [Plus a List of Free SQL Courses]

    Most Popular

    NLP Preprocessing using Spacy

    Machine Learning

    24. Node.js Lessons.Reading Parameters From the Command Line and Environment.

    Programming

    Работа с заказчиком

    Wiki
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

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