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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:27:28+00:00 2026-06-12T17:27:28+00:00

I like to get the latest enter and exit timestamp for a given user

  • 0

I like to get the latest enter and exit timestamp for a given user and for a given location. The collection is like this

{ "ActivityList" : [ 
{ "type" : "exit",
      "timestamp" : Date( 1348862537170 ),
      "user" : { "$ref" : "userProfile",
        "$id" : ObjectId( "4fdeaeeede26fd298262bb80" ) } }, 
    { "type" : "entry",
      "timestamp" : Date( 1348862546966 ),
      "user" : { "$ref" : "userProfile",
        "$id" : ObjectId( "4fdeaeeede26fd298262bb80" ) } }, 
       { "type" : "entry",
      "timestamp" : Date( 1348870744386 ),
      "user" : { "$ref" : "userProfile",
        "$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } }, 
    { "type" : "exit",
      "timestamp" : Date( 1348878233785 ),
      "user" : { "$ref" : "userProfile",
        "$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } } ],
  "Location" : { "$ref" : "loc",
    "$id" : ObjectId( "4fd410f0e7e994b59054b824" ) },
  "_id" : ObjectId( "4fe8f3c6e7e9ebe3697ee836" ) }

I tried something like this but does not work

db.collection.group(
{
    keyf: function(doc) {
        return {
            location    :doc.Location._id,
             userid     : doc.ActivityList.user._id,           
             actiontype : doc. ActivityList.type
        };
    },
    reduce: function(obj,prev) {
        if (prev.maxdate < obj. ActivityList.timestamp) { 
            prev.maxdate = obj. ActivityList.timestamp; 
        } 
    },
    initial: {maxdate:0}
});

Thanks for your help.

  • 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-12T17:27:29+00:00Added an answer on June 12, 2026 at 5:27 pm

    A simple $group won’t work for your data structure and finding/filtering the maximum values within an array. You would have to iterate the array to find the maximum values, which would be more efficiently done by retrieving the document and iterating in your application code.

    A possible server query approach in MongoDB 2.2 would be to use the new Aggregation Framework:

    db.activity.aggregate(
    
        // Find matching location documents first (can take advantage of index)
        { $match : {
            Location: {
                "$ref" : "loc", 
                "$id" : ObjectId("4fd410f0e7e994b59054b824")
            }
        }},
    
        // Unwind the ActivityList arrays as a document stream
        { $unwind : "$ActivityList" },
    
        // Filter activities to the user reference of interest
        { $match : {
           'ActivityList.user': {
                "$ref" : "userProfile",
                "$id" : ObjectId("4fdeaeeede26fd298262bb80")
            } 
        }},
    
        // Group the stream by activity types, and get the timestamp for the latest of each
        { $group : {
            _id : "$ActivityList.type",
            latest: { $max: '$ActivityList.timestamp' }
        }}
    )
    

    Sample result:

    {
        "result" : [
            {
                "_id" : "entry",
                "latest" : ISODate("2012-09-28T20:02:26.966Z")
            },
            {
                "_id" : "exit",
                "latest" : ISODate("2012-09-28T20:02:17.170Z")
            }
        ],
        "ok" : 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to get this user's OS language as a ISO-639-2 (3 letter language
How to get latest tag name (like version) of current branch?
I'm doing something like this to get the tweets from a list in twitter:
So, I'm using Facebox 1.3 (latest) I call facebox with a link like this:
I would like to get latest tags from main TYPO3 repository. TYPO3 proposes short
Query : I like to know how one can get latest entry of a
I'd like Visual Studio 2008 to do a get latest on the solution when
I have a blog in twitter like twitter.com/myname and I want to get latest
I need to get the latest date from coredata i found a way NSSortDescriptor
How do I get the latest version of my solution recursively like its done

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.