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

  • Home
  • SEARCH
  • 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 9254171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:24:00+00:00 2026-06-18T11:24:00+00:00

I am trying to use the aggregation function to display info in a chart.

  • 0

I am trying to use the aggregation function to display info in a chart. For this example, a document in the collection looks like this (excluding unnecessary fields for this query):

{
 'locid' : <someid>, #Reference to a city & state collection
 'collat' : <dateobj>, #a date object when this entry was saved 
 'pid' : <someid>, #Reference to a person collection 
 'pos' : <int> #Value I am interested in matching with location & date
 }

So I basically start with a pid. I use this as my first $match parameter to limit the amount of data that gets thrown into the pipeline.

array(
    '$match' => array(
         'pid' => new \MongoId($pid)
     )
),

So now that I have selected the correct pid, I tell it I only want/need certain fields:

array(
    '$project' => array(
        'pos' => 1,
        'collat' => 1,
        'locid' => 1
    )
),    

The second match is to say I only care about these locations right now ($ids contains an array of locid):

    array(
        '$match' => array(
            'locid' => array('$in' => $ids)
        )
    ),

And finally, I am saying group all the returned documents by collat and locid

array(
            '$group' => array(
                '_id' => array(
                    'locid' => '$locid',
                    'collat' => '$collat'
                )
            )
        )

While the query completes OK and returns data, I am not getting the pos field back, it is only returning the locid and collat.

Questions

  1. Isn’t that what $project is for? I use it to tell the driver what fields I want returned?
  2. Once I get the pos field returning as well, how can I tell the driver I only want the lowest value for each locid & collat combo pair? So say there are two entries for that date, location, and person: 4 & 8. I only care about pos=4

My end goal is to create a line chart with the X-Axis as the dates (from collat) and the Y-Axis will be the pos field, and each line will plot individual locid data.

Here is the entire parameters being sent to the aggregation driver.

$ops = array(
        array(
            '$match' => array(
                'pid' => new \MongoId($pid)
            )
        ),
        array(
            '$project' => array(
                'pos' => 1,
                'collat' => 1,
                'locid' => 1
            )
        ),
        array(
            '$match' => array(
                'locid' => array('$in' => $ids)
            )
        ),
        array(
            '$group' => array(
                '_id' => array(
                    'locid' => '$locid',
                    'collat' => '$collat'
                )
            )
        )
    );
    $out = $myCollection->aggregate($ops);

Update This is the way I got it to group & return pos without throwing an error. I need to spot check it though to make sure it’s actually returning the correct values though.

array(
            '$group' => array(
                '_id' => array(
                    'locid' => '$locid',
                    'collat' => '$collat'
                ),
                array('$min' => '$pos')
            )
        )
  • 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-18T11:24:01+00:00Added an answer on June 18, 2026 at 11:24 am

    Aggregation query is like an SQL statement group by. You are telling {$group} what field(s) you want to ‘GROUP BY’ but you are not telling it how you want to aggregate the grouped information.

    The {$group} you want is probably something like:

    {$group : { _id : { locid: "$locid", collat: "$collat"},
                pos : {$min : "$pos"}
               }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use this example http://www.sajithmr.me/jrecorder/example2.html for recording audio and send it to
I'm trying to use nested aggregation for one of my queries: SELECT t.type, t.avgrent
I am need paint my image. I'm trying use JQuery in here this link:
I've been trying to use the PHP MongoDB driver with the aggregation framework to
I'm trying use Places API from Google frmo a node.js server This is the
I'm trying to use the aggregate function with cbind , but I must be
I am trying to implement a function approximator (aggregation) using a rule-based fuzzy control
i'm trying use webview to load a image from sdcard i use this path
I am trying to use Mule 3.2.0 s Collection Aggregator. I tried using Mule
I am trying use the onClick() function in an activity for an android app.

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.