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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:51:51+00:00 2026-06-13T08:51:51+00:00

I’m attempting to use use the new MongoDB aggregation features to tally some statistics

  • 0

I’m attempting to use use the new MongoDB aggregation features to tally some statistics by date. Below is a sample of the documents that I am working with, my attempted code and desired result. The aggregation function retuns “UNDEFINED”. Can someone tell me why that is? And secondly, I want my aggregation function to group results by date in mm-dd-yyyy format. However as it is currently written I think the code is going to execute the aggregation by the full ISO date. Can someone please tell me how to fix this?

DOCUMENT EXAMPLE

{
  user: "2A8761E4-C13A-470E-A759-91432D61B6AF-25982-0000352D853511AF",
  language: "English",
  imageFileName: "F7A5ED9-D43C-4671-A5C6-F06C7E41F902-7758-000008371FB5B834",
  audioFileName: "F6D5727D-9377-4092-A28A-AA900F02653D-7758-0000083749066CF2",
  date: ISODate("2012-10-22T02:43:52Z"),
  correct: "1",
  _id: ObjectId("5084b2e8179c41cc15000001")
}

AGGREGATION FUNCTION

    var getUserStats = function(user, language, callback) {
    var guessCollection = db.collection('Guesses');
    guessCollection.aggregate(
  { $match: {
    user: user,
    language: language,
  }},
  { $sort: {
    date: 1
  }},
  { $project : {
     user : 1,
            language : 1,
            date : 1,
            correct : 1,
            incorrect : 1,
  } },
  { $unwind : "$language" },
  { $group : {
     _id : "$date",
            correct : { $sum : "$correct" },
            incorrect : { $sum : "$incorrect" }
  } }
, function(err, result){
    console.log(result);
    callback(result);
});

DESIRED RESULT

  {
        "result" : [
    //...snip...
            {
                "_id" : "2A8761E4-C13A-470E-A759-91432D61B6AF-25982-0000352D853511AF",
                "correct" : 32,
                "incorrect" : 17,
                "date" : 2012-10-22
            },
{
                "_id" : "2A8761E4-C13A-470E-A759-91432D61B6AF-25982-0000352D853511AF",
                "correct" : 16,
                "incorrect" : 7,
                "date" : 2012-10-23
            }
        ],
        "Ok" : 1
    }
  • 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-13T08:51:52+00:00Added an answer on June 13, 2026 at 8:51 am

    Regarding your first question about it returning undefined, there are two problems:

    1. You are using the $unwind operator on a field ($language) that isn’t an array.
    2. You are using the $sum operator on a string field ($correct); that’s only supported for number fields.

    For your second question about grouping on just the date, you need to project the date components you want to group on and then use those components in your $group operator’s _id value:

    For example:

    test.aggregate(
        { $match: {
            user: user,
            language: language
        }},
        { $sort: {
            date: 1
        }},
        { $project : {
            user : 1,
            language : 1,
            year : { $year: '$date' },
            month : { $month: '$date' },
            day : { $dayOfMonth: '$date'},
            correct : 1,
            incorrect : 1
        }},
        { $group : {
            _id : { year: "$year", month: "$month", day: "$day" },
            correct : { $sum : "$correct" },
            incorrect : { $sum : "$incorrect" }
        }},
        function(err, result){
            console.log(result);
        }
    );
    

    Produces output of:

    [ { _id: { year: 2012, month: 10, day: 22 },
        correct: 0,
        incorrect: 0 } ]
    

    You can assemble that into '2012-10-22' in code from there.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.