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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:18:35+00:00 2026-06-17T06:18:35+00:00

I have a database of ISSUES in MongoDB, some of the issues have comments,

  • 0

I have a database of ISSUES in MongoDB, some of the issues have comments, which is an array; each comments has a writer. How can I count the number of comments each writer has written?

I’ve tried

db.test.issues.group(
{
    key = "comments.username":true;
    initial: {sum:0},
    reduce: function(doc, prev) {prev.sum +=1},
    }
);

but no luck 🙁

A Sample:

{
        "_id" : ObjectId("50f48c179b04562c3ce2ce73"),
        "project" : "Ruby Driver",
        "key" : "RUBY-505",
        "title" : "GETMORE is sent to wrong server if an intervening query unpins the connection",
        "description" : "I've opened a pull request with a failing test case demonstrating the bug here: https://github.com/mongodb/mongo-ruby-driver/pull/134\nExcerpting that commit message, the issue is: If we do a secondary read that is large enough to require sending a GETMORE, and then do another query before the GETMORE, the secondary connection gets unpinned, and the GETMORE gets sent to the wrong server, resulting in CURSOR_NOT_FOUND, even though the cursor still exis ts on the server that was initially queried.",
        "status" : "Open",
        "components" : [
                "Replica Set"
        ],
        "affected_versions" : [
                "1.7.0"
        ],
        "type" : "Bug",
        "reporter" : "Nelson Elhage",
        "priority" : "major",
        "assignee" : "Tyler Brock",
        "resolution" : "Unresolved",
        "reported_on" : ISODate("2012-11-17T20:30:00Z"),
        "votes" : 3,
        "comments" : [
                {
                        "username" : "Nelson Elhage",
                        "date" : ISODate("2012-11-17T20:30:00Z"),
                        "body" : "Thinking some more"
                },
                {
                        "username" : "Brandon Black",
                        "date" : ISODate("2012-11-18T20:30:00Z"),
                        "body" : "Adding some findings of mine to this ticket."
                },
                {
                        "username" : "Nelson Elhage",
                        "date" : ISODate("2012-11-18T20:30:00Z"),
                        "body" : "I think I tracked down the 1.9 dependency."
                },
                {
                        "username" : "Nelson Elhage",
                        "date" : ISODate("2012-11-18T20:30:00Z"),
                        "body" : "Forgot to include a link"
                }
        ]
}
  • 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-17T06:18:37+00:00Added an answer on June 17, 2026 at 6:18 am

    You forgot the curly braces on the key value and you need to terminate that line with a , instead of a ;.

    db.issues.group({
        key: {"comments.username":true},
        initial: {sum:0},
        reduce: function(doc, prev) {prev.sum +=1},
    });
    

    UPDATE

    After realizing comments is an array…you’d need to use aggregate for that so that you can ‘unwind’ comments and then group on it:

    db.issues.aggregate(
        {$unwind: '$comments'},
        {$group: {_id: '$comments.username', sum: {$sum: 1}}}
    );
    

    For the sample doc in the question, this outputs:

    {
      "result": [
        {
          "_id": "Brandon Black",
          "sum": 1
        },
        {
          "_id": "Nelson Elhage",
          "sum": 3
        }
      ],
      "ok": 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one-shot Node script that makes some changes to a MongoDB database
If I have a database with some sharded and unsharded collections. Can I direct
I have a web application that is having some database issues and I'm looking
At our organization we have a SQL Server 2005 database and a fair number
I have a database winforms application using SQLite written in C#. I'm attempting to
I have created a mongodb database. I have a list of clients who are
I have a database which was upgraded from 2000 to 2005. Is there any
OK, so I have a database field of type char(1) that has a small
I have been tasked with porting data from a MongoDB database to a MySQL
We have a database with a bunch of wide tables (40-80 columns each) and

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.