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 8965733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:52:26+00:00 2026-06-15T16:52:26+00:00

I have a collection of documents like this one: { _id : ObjectId(…), field1:

  • 0

I have a collection of documents like this one:

{
    "_id" : ObjectId("..."),
    "field1": "some string",
    "field2": "another string",
    "field3": 123
}

I’d like to be able to iterate over the entire collection, and find the entire number of fields there are. In this example document there are 3 (I don’t want to include _id), but it ranges from 2 to 50 fields in a document. Ultimately, I’m just looking for the average number of fields per document.

Any ideas?

  • 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-06-15T16:52:27+00:00Added an answer on June 15, 2026 at 4:52 pm
    PRIMARY> var count = 0;
    PRIMARY> db.my_table.find().forEach( function(d) { for(f in d) { count++; } });
    PRIMARY> count
    1074942
    

    This is the most simple way I could figure out how to do this. On really large datasets, it probably makes sense to go the Map-Reduce path. But, while your set is small enough, this’ll do.

    This is O(n^2), but I’m not sure there is a better way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one document in my params collection like this: { _id: ObjectId(4d124cef3ffcf6f410000037), code:
We have some embedded documents that look like this: { _id : ObjectId(4e402353bc9f6ec5a6000001), first_name
I have 10000 documents in one MongoDB collection. I'd like to update all the
Consider I have two collections in MongoDB. One for products with documents like: {'_id':
I have many collections of documents in a mongo database that look like this:
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have a collection with 1.5 million documents. I'm counting using PHP: $db->some->ensureIndex(array(sometext =>
I have a schema, with a document structure that looks like this: { _id
I have a collection of documents in MongoDB where each has one or more
Lets say I have collection of documents with specified longitude and latitude. type is

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.