Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:08:34+00:00 2026-05-28T04:08:34+00:00

If I have a collection with thousands of elements, is there a way I

  • 0

If I have a collection with thousands of elements, is there a way I can easily find which elements are taking up the most space (in terms of MB)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:08:35+00:00Added an answer on May 28, 2026 at 4:08 am

    There’s no built-in query for this, you have to iterate the collection, gather size for each document, and sort afterwards. Here’s how it’d work:

    var cursor = db.coll.find(); 
    var doc_size = {}; 
    cursor.forEach(function (x) { 
        var size = Object.bsonsize(x); 
        doc_size[x._id] = size;
    });
    

    At this point you’ll have a hashmap with document ids as keys and their sizes as values.
    Note that with this approach you will be fetching the entire collection over the wire. An alternative is to use MapReduce and do this server-side (inside mongo):

    > function mapper() {emit(this._id, Object.bsonsize(this));}
    > function reducer(obj, size_in_b) { return { id : obj, size : size_in_b}; }
    >
    > var results = db.coll.mapReduce(mapper, reducer, {out : {inline : 1 }}).results
    > results.sort(function(r1, r2) { return r2.value - r1.value; })
    

    inline:1 tells mongo not to create a temporary collection for results, everything will be kept in RAM.

    And a sample output from one of my collections:

    [
        {
            "_id" : ObjectId("4ce9339942a812be22560634"),
            "value" : 1156115
        },
        {
            "_id" : ObjectId("4ce9340442a812be24560634"),
            "value" : 913413
        },
        {
            "_id" : ObjectId("4ce9340642a812be26560634"),
            "value" : 866833
        },
        {
            "_id" : ObjectId("4ce9340842a812be28560634"),
            "value" : 483614
        },
           ...
        {
            "_id" : ObjectId("4ce9340742a812be27560634"),
            "value" : 61268
        }
    ]
    > 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a collection of thousands of objects, all of which implement
I have Class and Student objects. Both have collection of another as property. Which
I have a collection that use as a cache. How can I limit the
Background I have a large collection (~thousands) of sequences of integers. Each sequence has
I am having an empty collection and have thousands of entries to process (entries
I have a collection of Posts and Users where users can upvote/downvote each post.
I have a collection with thousands of nodes. I'd like each node to fire
I have thousands of products which have some common default values: page layout :
I have a large (in thousands) collection of word : value (float) pairs. I
I have a collection of std::set . I want to find the intersection of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.