By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Tracking Mongoose Query Times with a Few Lines of Code | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Tracking Mongoose Query Times with a Few Lines of Code | HackerNoon
Computing

Tracking Mongoose Query Times with a Few Lines of Code | HackerNoon

News Room
Last updated: 2025/05/22 at 12:19 PM
News Room Published 22 May 2025
Share
SHARE

I recently found myself working on a little side project and needed a way to log the mongoose query and keep track of how long the queries took, because they took a long time.

Ideally, you’d have an APM tool like Sentry or otherwise set up, but that would have been overkill, so this article is my lazy approach to building a MongoDB/Mongoose profiler.

USER SCHEMA

Given a generic User details schema as below

// user.schema.ts

type IUser {
 _id: string;
 name: string;
 email: string;
}

const UserSchema = new Schema<IUser>(
{
  name: { type: String, required: true },
  email: { type: String, required: true }
},
{ timestamps: true }
)

const user = model<IUser>('user', UserSchema);

This schema is a generic user schema to keep track of the user name and email, which is quite adequate for our use case.

THE PROFILER

The code block below is the implementation of the basic profiler

// mongoose-profiler.ts

const redactConditionsValue = (conditions: Record<string, any>): void => {
  const keys = Object.keys(conditions);
  keys.forEach((key) => {
    conditions[key] = 'REDACTED';
  });
};

export const mongooseProfiler = (schema: Schema) => {
  schema.pre(/^find/, function (this: any, next: () => void) {
    this._startTime = process.hrtime();
    next();
  });

  schema.post(/^find/, function (this: any, _result: any, next: () => void) {
    const start = this._startTime;
    if (start) {
      const diff = process.hrtime(start);
      const durationMs = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(2);
      const modelName = this.model?.modelName || 'UnknownModel';
      const operation = this.op || 'unknownOp';

      redactConditionsValue(this._conditions);
      logger.info(`[Mongoose] ${modelName}.${operation}, conditions: ${JSON.stringify(this._conditions)} duration ${durationMs}ms`);
    }
    next();
  });

  schema.pre('distinct', function (this: any, next: () => void) {
    this._startTime = process.hrtime();
    next();
  });

  schema.post('distinct', function (this: any, _result: any, next: () => void) {
    const start = this._startTime;
    if (start) {
      const diff = process.hrtime(start);
      const durationMs = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(2);
      const modelName = this.model?.modelName || 'UnknownModel';
      const operation = this.op || 'unknownOp';

      redactConditionsValue(this._conditions);
      logger.info(
        `[Mongoose] ${modelName}.${operation}, conditions: ${JSON.stringify(this._conditions)}, distinct: ${this._distinct} duration ${durationMs}ms`
      );
    }
    next();
  });

  schema.pre('aggregate', function (this: any, next: () => void) {
    this._startTime = process.hrtime();
    next();
  });

  schema.post('aggregate', function (this: any, _result: any, next: () => void) {
    const start = this._startTime;
    if (start) {
      const diff = process.hrtime(start);
      const durationMs = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(2);
      const modelName = this._model?.modelName || 'UnknownModel';
      redactConditionsValue(this.pipeline());
      logger.info(`[Mongoose] ${modelName}.aggregate, pipeline: ${JSON.stringify(this.pipeline())} duration ${durationMs}ms`);
    }
    next();
  });
};

EXPLANATION

The profiler makes use of the available mongoose pre and post middleware hooks, the start time is stored before the query execution begins, when the pre hook is called, the duration calculation then happens in the post hook.

The redactConditionsValue was added to redact query information, because by default, Mongoose would log your query conditions, which is not ideal if you are building a production-ready system.

You can add hooks for create and update functions if that suits your use case.

USAGE

To the profiler, you add the following code to your schema file

mongoooseProfiler(UserSchema);

OUTPUT

This is what the log looks like: it contains the model, operation, a redacted query, and duration in milliseconds

[Mongoose] User.findOne, conditions: {"_id":"REDACTED"} duration 40.59ms

CONCLUSION

For the scale of the project I was working on, this allows me to know which queries are been run and which queries are taking a long time to execute, so I can take note to optimise them, consequently improving performance.

Feel free to reach out if you have any questions.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Google Home is getting a major Gemini upgrade
Next Article ETH and XRP Already Topped Out, The Real Winner of 2026 is This Viral Passive Income Platform
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Noise, Numbers, and the New Normal for U.S. Census Data | HackerNoon
Computing
Anthropic’s New Model Excels at Reasoning and Planning—and Has the Pokémon Skills to Prove It
Gadget
Democrats rip Trump ahead of meme coin dinner: 'Orgy of corruption'
News
The AI Maestro: Ahmad Bouka on Turning a Modest Chatbot into a Company-Wide Intelligence Engine
Gadget

You Might also Like

Computing

Noise, Numbers, and the New Normal for U.S. Census Data | HackerNoon

6 Min Read
Computing

Beyond the Usual Doom: Five AI Dangers Nobody Is Talking About | HackerNoon

4 Min Read
Computing

Transparency’s Double-Edged Sword in Census Privacy | HackerNoon

12 Min Read
Computing

Swarm Robotics: The Future Belongs to the Collective | HackerNoon

6 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?