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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:02:55+00:00 2026-06-06T05:02:55+00:00

I have mongo documents in this format. {_id : 1,Summary : {…},Examples : [{_id

  • 0

I have mongo documents in this format.

{"_id" : 1,"Summary" : {...},"Examples" : [{"_id" : 353,"CategoryId" : 4},{"_id" : 239,"CategoryId" : 28}, ...  ]}
{"_id" : 2,"Summary" : {...},"Examples" : [{"_id" : 312,"CategoryId" : 2},{"_id" : 121,"CategoryId" : 12}, ...  ]}

How can I map/reduce them to get a hash like:

{ [ result[categoryId] : count_of_examples , .....] }

I.e. count of examples of each category.
I have 30 categories at all, all specified in Categories collection.

  • 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-06T05:02:59+00:00Added an answer on June 6, 2026 at 5:02 am

    If you can use 2.1 (dev version of upcoming release 2.2) then you can use Aggregation Framework and it would look something like this:

    db.collection.aggregate( [
           {$project:{"CatId":"$Examples.CategoryId","_id":0}}, 
           {$unwind:"$CatId"}, 
           {$group:{_id:"$CatId","num":{$sum:1} } },  
           {$project:{CategoryId:"$_id",NumberOfExamples:"$num",_id:0  }} 
    ] );
    

    The first step projects the subfield of Examples (CategoryId) into a top level field of a document (not necessary but helps with readability), then we unwind the array of examples which creates a separate document for each array value of CatId, we do a “group by” and count them (I assume each instance of CategoryId is one example, right?) and last we use projection again to relabel the fields and make the result look like this:

    "result" : [
        {
            "CategoryId" : 12,
            "NumberOfExamples" : 1
        },
        {
            "CategoryId" : 2,
            "NumberOfExamples" : 1
        },
        {
            "CategoryId" : 28,
            "NumberOfExamples" : 1
        },
        {
            "CategoryId" : 4,
            "NumberOfExamples" : 1
        }
    ],
    "ok" : 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many collections of documents in a mongo database that look like this:
How can I have a mongo query similar to the SQL ...WHERE _id >
Lets say I have mongo documents like this: Question 1 { answers:[ {content: 'answer1'},
I have a mongo collection which has documents with two fields fieldA and fieldB,
I have an existing mongo database where all documents in a collection have a
I have documents in Mongo that contain questionnaire-like data (company's name, address, phone numbers,
I have a collection of the following sort of documents (taken from Mongo DB
We have millions of documents in mongo we are looking to index on solr.
I have to read an existing mongo database. Is mongoid suitable for this? All
If I create a collection in Mongo, and after adding documents to this collection

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.