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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:02:20+00:00 2026-06-12T08:02:20+00:00

This question has two parts. The collection structure is: _id: MongoID, agent_id: string, result:

  • 0

This question has two parts. The collection structure is:

_id: MongoID,
agent_id: string,
result: string,
created_on: ISO DATE,
…other fields…

Part one:
Desired Output: One result for each agent_id and result combination with a count: TUPLE representation with Equivalent SQL using PostgreSQL.

( "1234", "Success", 4 ),
( "1234", "Failure", 4 ),
( "4567", "Success", 3 ),
( "7896", "Failure", 2 ),
.....

SELECT agent_id, result, count(*)
FROM table
GROUP BY agent_id, result
HAVING created_on >= now()::date;

I have come up with the below mongo query….I think I have a conceptual or syntax error. The docs say to use $match early in the pipeline:, but although the $match limits the query when I run it by itself, as soon as I add the $group I get way to many results. Also I can’t seem to understand how to group by more than one field. How can I edit the below query to get results like the SQL query above?

db.collection.aggregate(
  { $match : 
    { created_on: 
        { $gte: new Date('08-13-2012') //some arbitrary date
    }
  }, $group:
    { _id:"$agent_id" }, 
   $project:
  {_id:0, agent_id:1, result:1}
})

Part 2)
The first result set would be adequate, but not optimal. With PostgreSQL I can achieve a result set like:

( "1234", { "Success", "Failure" }, { 4, 3 } ),
( "4567", { "Success", "Failure" }, { 3, 0 } ),
( "7896", { "Success", "Failure" }, { 0, 2 } )

I can do this in Postgresql with the array data type and a set_to_array function (custom function). The Pg specific SQL is:

SELECT agent_id, set_to_array(result), set_to_array( count(*) )
FROM table
GROUP BY agent_id, result
HAVING created_on >= now()::date;

I believe the equivalent data structure in mongodb would look like :

[
   { "1234", [ { "success": 4 }, { "failure": 4 } ] },
   { "4567", [ { "success": 3 }, { "failure": 0 } ] },
   { "7896", [ { "success": 0 }, { "failure": 0 } ] }
]

Is it possible to achieve these desired compressed results with mongodb aggregate framework ?

  • 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-12T08:02:21+00:00Added an answer on June 12, 2026 at 8:02 am

    Here you go:

    Created some test data:

    db.test.insert({agent_id:”1234″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1234″, result:”Success”, created_on:new Date()});
    db.test.insert({agent_id:”1234″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1234″, result:”Success”, created_on:new Date()});
    db.test.insert({agent_id:”1234″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1234″, result:”Success”, created_on:new Date()});
    db.test.insert({agent_id:”1234″, result:”Success”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Success”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Success”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Success”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Success”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Failure”, created_on:new Date()});
    db.test.insert({agent_id:”1324″, result:”Failure”, created_on:new Date()});

    db.test.aggregate(
      {
        $match:{ /* filter out the things you want to aggregate */
          created_on:{$gte:new Date(1000000)}
        }
      }, 
      {
        $group: {_
          _id: { /* the things you want to group on go in the _id */
            agent_id:"$agent_id", 
            result:"$result"
          }, 
          count:{$sum:1} /* simple count */
        }
      }, 
      {
        $project: { /* take the id out into the separate fields for your tuple. */
          _id:0, 
          agent_id:"$_id.agent_id", 
          result:"$_id.result", 
          count:"$count"
        }
      });
    

    Gives:

    {
    "result" : [
        {
            "count" : 7,
            "agent_id" : "1324",
            "result" : "Failure"
        },
        {
            "count" : 4,
            "agent_id" : "1324",
            "result" : "Success"
        },
        {
            "count" : 4,
            "agent_id" : "1234",
            "result" : "Success"
        },
        {
            "count" : 3,
            "agent_id" : "1234",
            "result" : "Failure"
        }
    ],
    "ok" : 1
    } 
    

    Adding part 2–pretty similar to part 1, but the counting is a bit more complicated; basically you count only if it matches what you want to count:

    db.test.aggregate(
      {
        $match: { 
          created_on: {$gte:new Date(1000000)}
        }
      }, 
      {
        $group: {
          _id: { 
            agent_id:"$agent_id"
          }, 
          failure: {
            $sum:{
              $cond:[
                {$eq:["$result","Failure"]}, 
                1, 
                0
              ]
            }
          }, 
          success: {
            $sum: { 
              $cond:[
                {$eq:["$result","Success"]}, 
                1, 
                0
              ]
            }
          } 
        } 
      }, 
      {
        $project: {
          _id: 0, 
          agent_id: "$_id.agent_id", 
          failure: "$failure", 
          success: "$success"
        }
      });
    

    Gives:

    {
    "result" : [
        {
            "failure" : 7,
            "success" : 4,
            "agent_id" : "1324"
        },
        {
            "failure" : 3,
            "success" : 4,
            "agent_id" : "1234"
        }
    ],
    "ok" : 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has two parts. Part 1. Yesterday I had some code which would
This question has two parts (maybe one solution?): Sample vectors from a sparse matrix
This question actually has two parts. The first part: I've been developing my first
This question has two parts: #1 I have a functions.php that is filled with
This question has two parts. First, I need to isolate two strings of text
My question has two parts regarding haproxy. Question 1: The service that I am
This question is in two parts: A) Is my design wrong? If so, what
My question has two parts. First one is: How can I include the background
Two parts to this question 1) I am working with a group of six
This question has developed off an answer here . My question therefore is what

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.