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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:36:04+00:00 2026-05-29T03:36:04+00:00

I want to automatically update a LastUpdatedOn field on a mongo document every time

  • 0

I want to automatically update a LastUpdatedOn field on a mongo document every time a save or update operation occurs on that document.

Rather than putting this burden on every bit of code that does a save/update, is there some way to automatically do this (I’m using the C# driver).

For e.g. in nHibernate you can use interceptors. Is there a similar technique available at either the database or driver level?

  • 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-05-29T03:36:05+00:00Added an answer on May 29, 2026 at 3:36 am

    interceptors. Is there a similar technique available at either the database or
    driver level?

    There is no such mechanism in mongodb c# driver and in the mongodb as well.
    To achieve your needs you can simple wrap all Save, Insert, atomic Update methods and set LastUpdatedOn field there.

    For example you can have base class for your repository, service, whatever where you will wrap your Save methods:

    public abstract class BaseMongoService<T> where T : BaseDocument
    {
       protected abstract MongoCollection Items { get; }
    
       public virtual SafeModeResult Save(T document)
       {
          document.LastUpdatedOn = DateTime.Now;
    
          return Items.Save(document);
       }
       public virtual void Update(IMongoQuery query, IMongoUpdate update)
       {
          update = update.Set("LastUpdatedOn", DateTime.Now);
          Items.Update(query, update);
       }
    }
    

    then you will need inherit all your documents from BaseDocument (or force them implement some interface) to add constraint that all documents should contains LastUpdatedOn field.

    public class BaseDocument
    {
      public DateTime LastUpdatedOn {get;set;}
    }
    

    Then just inherit all data access classes from above one and use Save, Update methods from base class.

    Note: all this code was not tested, it just how i see it can be done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an index view that I want to update automatically as the user
I have made an out-of-browser silverlight application which I want to automatically update every
We are using a ticketing system that I want to automatically update as developers
I want Windows Update to automatically download and install updates on my Vista machine,
I have an access macro, that I want to run automatically from a batch
I have a form, and I want automatically to update it when I click
I want to visit a url via http://xxxxx.php then my server will update automatically
I have an image and I want to automatically update it using either javascript,
Is there a way to make a label automatically update itself so that I
I want to automatically commit the database when we copy files from one branch

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.