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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:34:33+00:00 2026-06-03T18:34:33+00:00

If I remember correctly the ObjectId of a MongoDB document contains the timestamp of

  • 0

If I remember correctly the ObjectId of a MongoDB document contains the timestamp of its creation. What would be the best way to update only the most recent document in a collection?

So say that I have a bunch of game documents and every so often a new document is created and every score should be saved to the most recent game, not to the previous games. I would like to do this in order to not store the game object in memory.

  • 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-03T18:34:35+00:00Added an answer on June 3, 2026 at 6:34 pm

    I’ve tried a couple of things and I couldn’t make it in single operation. However, you don’t need to store whole game object in memory. You can just ask for the latest _id. Something like this (shell pseudo syntax).

    var last_id = db.games.find({}, {_id: 1}).sort({_id: -1}).limit(1);
    db.games.update({_id: last_id}, {$inc: {score: 1}});
    

    Update:

    Okay, I should probably go to sleep now. Of course, it can be done using findAndModify. Here’s JS code.

    db.runCommand({findAndModify: 'games', 
                   sort: {_id: -1}, 
                   update: {$inc: {score: 1}}})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I remember correctly Python ships with Tk as it's standard framework. What's Ruby's
NOTE: I'm using a BSD based system so if I remember correctly this means
Remember imagemaps? They were all over the place in the 90s. A webpage would
Possible Duplicate: Space Before Closing Slash? Hi, If I remember correctly we have it
I am not sure if I remember correctly, but I think I read once
I can't seem to find out anywhere but if I remember correctly there was
In Donald Knuth's Literate Programming , there was if I remember correctly a graph
If I remember correctly, in past versions of Visual Studio (6, 2003, 2005), if
If I remember correctly, modern cameras send out an ultrasound wave and measure time
If i remember correctly in .NET one can register global handlers for unhandled exceptions.

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.