Mongoose 'static' methods vs. 'instance' methods
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Mongoose, both static and instance methods let you attach domain logic to a schema, but they solve different problems. Static methods operate at model scope, while instance methods operate on one document. Choosing the right method type keeps your data layer predictable and prevents awkward code that mixes query logic with record mutation.
Static Methods: Model-Level Operations
A static method belongs to the model, so you call it as User.someMethod(). Use statics for queries, aggregations, and factory-style operations that do not require a preloaded document.
This keeps query rules close to the model and avoids repeated filter snippets across services.
Instance Methods: Document-Level Behavior
An instance method runs on a document already loaded from the database. Use instance methods when logic depends on current document state, especially for updates and computed checks.
A good rule is simple: if you need this document fields, use an instance method.
Choosing the Right Method Type
Use statics when input is external and you need a document or list of documents. Use instance methods when input is mostly the current document and action mutates or inspects it.
Typical static use cases:
- Lookup by business key.
- Collection-level reports.
- Paginated list queries.
Typical instance use cases:
- State transitions such as activate or deactivate.
- Field normalization before save.
- Behavior that naturally belongs to a single record.
Keeping this separation clear makes code review easier because call sites communicate intent.
Sessions and Transactions
Both method types can support transactions. Pass a session to static queries and instance saves to keep operations atomic.
When method design is clean, adding session support later is straightforward.
Testing Strategy
Test statics with seeded collections and assert query behavior. Test instance methods with concrete documents and assert state changes after save.
Avoid testing only return values for instance methods. Always verify persisted state.
Common Pitfalls
- Putting query-heavy logic in instance methods, which forces unnecessary document loading.
- Using arrow functions for methods that rely on
this, which breaks context. - Duplicating identical filters in controllers instead of centralizing them in statics.
- Saving documents inside statics without clear transaction boundaries.
- Mixing validation, side effects, and persistence in one oversized method.
Summary
- Static methods are model-level and best for collection queries and lookups.
- Instance methods are document-level and best for record-specific behavior.
- A clear split between static and instance responsibilities improves maintainability.
- Both method types can participate in sessions and transactions.
- Test statics for query correctness and instance methods for persisted state changes.
Related reading
- Mongoose unique field
- Monitoring pending async operations in Node.js promised environment
- monk vs mongoose for Mongodb
- Most efficient way to create a zero filled JavaScript array?
- Most efficient way to escape XML/HTML in C string?
- Multiple asynchronous Ajax calls inside each loop in Jquery
- Multiple Awaits in a single method
- Multiple images inside one container
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.