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

The Archive Base Latest Questions

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

Does anyone have a good way to run a group function in PHP with

  • 0

Does anyone have a good way to run a group function in PHP with a DISTINCT count? The situation is this: I want to check unique logins to our app. This is how the document in the current collection I’m querying looks like:

Array
(
    [_id] => MongoId Object
        (
            [$id] => 50f6da87686ba9f449000003
        )

    [userId] => 50f6bd0f686ba91a4000000f
    [action] => login
    [time] => 1358355079

What I would like to do is count the UNIQUE userIDs through a group statement by date. This is the group statement that I am using (in PHP):

$map= new MongoCode('function(frequency) {
                             var date = new Date(Number(frequency.time) * 1000 - (8 * 3600000));
                             var dd = date.getDate();
                             var yyyy = date.getFullYear();
                             var mm = date.getMonth() + 1;
                             if(dd<10){dd="0"+dd}
                             if(mm<10){mm="0"+mm}
                             var transday = yyyy + "-"+ mm + "-" + dd;
                             return{"date": transday}
                    }');
$initial = array('logins' => array());
$reduce = "function(obj, prev){
                prev.logins.push(obj.userId);
                     }";


try{
  $distinct = array();
  $g = $this->_bingomongo->selectCollection("UserLog")->group($map, $initial, $reduce);

From this point I am simply doing an array_unique in memory…but what I’d really like to do (and can’t find a good answer on the net) is run a distinct count in the group by. I have seen a couple of other examples where we can use an array(distinct=>'key') but I am looking to do a count. Any thoughts?

Here’s the rest of the code in it’s current (imperfect) format:

    foreach($g['retval'] as $k=>$v) {
          $distinct[date('m.d', strtotime($v['date']))] = array_unique($v['logins']);
      }
    }
    catch(Exception $e) {
       return $e;
    } return $g;

Happy to do this in map/reduce if strictly necessary- but would rather find a way to keep it in group by (as a COUNT(distinct)).

  • 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-18T00:12:25+00:00Added an answer on June 18, 2026 at 12:12 am

    You should be able to do something like this:

    ...
    $initial = array('logins' => array());
    $reduce = "function(obj, prev){
                   prev.logins[obj.userId] = 1;
               }";
    
    $finalize = "function(result) {
                     result.loginList = Object.keys(result.logins);
                     result.count = result.loginList.length;
                }";
    
    try{
        $distinct = array();
        $g = $this->_bingomongo->selectCollection("UserLog")->group(
            $map, $initial, $reduce, array('finalize'=>$finalize));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a good way to have code run after their Android
Does anyone have a good way of deleting duplicate transactions (same date, amount, biller,
Does anyone have a good VBA way (or perhaps there is an MS Word
Does anyone know of a good way to have a user enter an amount
Does anyone have a good solution for the following (potential) situation ? Using a
Does anyone have a good way to build MSI (vdproj) projects using MsBuild or
Does anyone have a good way to re-encode all the *.java file in a
Does anyone have any suggestions for a good way to call R from S-Plus?
DOes anyone have any good information with regards to when to log, I was
Does anyone have any good advice or experience on how to create an engine

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.