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

The Archive Base Latest Questions

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

How flexible is the aggregate function for output formatting in MongoDB? Data format: {

  • 0

How flexible is the aggregate function for output formatting in MongoDB?

Data format:

{
        "_id" : ObjectId("506ddd1900a47d802702a904"),
        "port_name" : "CL1-A",
        "metric" : "772.0",
        "port_number" : "0",
        "datetime" : ISODate("2012-10-03T14:03:00Z"),
        "array_serial" : "12345"
}

Right now I’m using this aggregate function to return an array of DateTime, an array of metrics, and a count:

{$match : { 'array_serial' : array, 
                            'port_name' : { $in : ports},
                            'datetime' : { $gte : from, $lte : to}
                        }
                },
               {$project : { port_name : 1, metric : 1, datetime: 1}},
               {$group : { _id : "$port_name", 
                            datetime : { $push : "$datetime"},
                            metric : { $push : "$metric"},
                            count : { $sum : 1}}}

Which is nice, and very fast, but is there a way to format the output so there’s one array per datetime/metric? Like this:

[
    {
      "_id" : "portname",
      "data" : [
                ["2012-10-01T00:00:00.000Z", 1421.01],
                ["2012-10-01T00:01:00.000Z", 1361.01],
                ["2012-10-01T00:02:00.000Z", 1221.01]
               ]
    }
]

This would greatly simplify the front-end as that’s the format the chart code expects.

  • 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-12T16:59:03+00:00Added an answer on June 12, 2026 at 4:59 pm

    Combining two fields into an array of values with the Aggregation Framework is possible, but definitely isn’t as straightforward as it could be (at least as at MongoDB 2.2.0).

    Here is an example:

    db.metrics.aggregate(
    
        // Find matching documents first (can take advantage of index)
        { $match : {
            'array_serial' : array, 
            'port_name' : { $in : ports},
            'datetime' : { $gte : from, $lte : to}
        }},
    
        // Project desired fields and add an extra $index for # of array elements
        { $project: {
            port_name: 1,
            datetime: 1,
            metric: 1,
            index: { $const:[0,1] }
        }},
    
        // Split into document stream based on $index
        { $unwind: '$index' },
    
        // Re-group data using conditional to create array [$datetime, $metric]
        { $group: {
            _id: { id: '$_id', port_name: '$port_name' },
            data: {
                $push: { $cond:[ {$eq:['$index', 0]}, '$datetime', '$metric'] }
            },
        }},
    
        // Sort results
        { $sort: { _id:1 } },
    
        // Final group by port_name with data array and count
        { $group: {
            _id: '$_id.port_name',
            data: { $push: '$data' },
            count: { $sum: 1 }
        }}
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to plot some data from Flexible Image Transport System (FITS) files,
I'm struggling to make a flexible data structure (like a relational database) for a
I have created a flexible navigation bar in my app that will show custom
I need to find a flexible solution for access control in PHP. In the
I want to have a flexible templating language that I can customize for a
I was looking for a flexible way to generate different sidebars in different pages.
I know WPF is more complex an flexible so could be thought to do
I thought I'd be flexible this time around and let the users decide what
All the examples I could find of flexible box model show stuff expanding either
I'm trying to create a flexible box layout that fills the entire viewport, 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.