MongoDB driver version compatibility guide
Overview
This document explains how monSQLize handles version differences in the MongoDB Node.js driver and how to ensure compatibility for future driver upgrades.
Current baseline: monSQLize uses mongodb@6.21.0 as the runtime dependency baseline; Driver 7.5.0 has passed extended compatibility verification. Driver 4.x / 5.x belongs to the historical compatibility background and is no longer part of the current support matrix.
Currently supported driver versions
Current support matrix
Dependency declaration
Statement in package.json:
Description:
- Users will get
mongodb@6.21.0by default after installing monSQLize, and there is no need to install additional MongoDB driver. - Driver 7.5.0 passed compatibility verification, but is not the default runtime dependency of the current package.
- If the driver is unavailable due to package manager trimming, overwriting dependencies, or workspace resolution, restore the complete installation first, and then execute the verification command in this article.
monSQLize's handling of version differences
1. Return value of findOneAnd* method
This is the most important difference. Currently monSQLize runs on MongoDB Driver 6.21.0 by default and verifies public API behavior through the Driver 7.5.0 extension matrix.
MongoDB driver version differences
Driver 4.x return format:
Driver 5.x return format:
Driver 6.x / 7.x default return format:
monSQLize current behavior
With the current monSQLize package dependency baseline, user code receives the document directly or null:
Implementation Boundary:
- monSQLize provides out-of-the-box behavior with
mongodb@6.21.0exact dependency. - Driver 7.5.0 passed
test:compatibilityand server matrix verification. - If the application is forced to overwrite the historical Driver 4.x / 5.x, you need to verify the return value difference by yourself. This is not recommended for new projects.
Applicable methods:
- findOneAndUpdate
- findOneAndReplace
- findOneAndDelete
2. includeResultMetadata explicit control
Features:
- Metadata is not returned by default, but the document or
nullis returned directly. - When
includeResultMetadata: trueis required, explicitly pass in the native MongoDB Driver option. - When migrating from old Driver metadata return values, avoid continuing to read
result.lastErrorObjectunconditionally.
Other affected methods
Note: Driver 4.x / 5.x is only used as a reference for historical migration. The current documentation no longer lists them as the normal user verification path.
monSQLize compatibility guarantee
Core strategy: precise dependency + thin packaging + matrix verification
monSQLize does not require users to manually install or select a MongoDB Driver. The current package reduces usage burden in the following ways:
package.jsondeclares exactlymongodb@6.21.0and is ready to use after installation.- The writing and querying API maintains thin encapsulation and transparently transmits the stable behavior of the current driver by default.
test:compatibilitywith server matrix covers the current baseline and Driver 7.5.0 extended validation.- Historical Driver 4.x / 5.x is only used as a migration reference, not as a current user path commitment.
Key implementation locations
Location: src/adapters/mongodb/writes/
Responsibilities:
- Call native
collection.findOneAndUpdate/findOneAndReplace/findOneAndDelete - Keep the current Driver’s default behavior of returning documents or
null - Provide monSQLize’s own encapsulation for extension methods such as batch writing, upsert, and increment.
- Discover upstream driver behavior drift through the test matrix
Core functions:
Version management mechanism
- Normal monSQLize usage does not require users to declare
mongodbdirectly. - Compatibility verification temporarily overwrites the driver version and restores
mongodb@6.21.0after verification. - CI compatibility checks should use
npm ls mongodbandnpm run test:compatibilityas evidence.
Exception handling
- When there is no matching document,
findOneAnd*returnsnull. - If the caller explicitly passes in
includeResultMetadata: true, the return value follows the MongoDB Driver native metadata structure. - If the application is overwritten as a historical driver and gets
{ value, ok, lastErrorObject }, you should first restore the package dependency baseline or complete migration verification at the application layer.
Future driver upgrade guide
Pre-upgrade checklist
When MongoDB releases a new major driver version (such as 8.x in the future), please follow these steps:
Step 1: Read the official documentation ✅
- Read the MongoDB driven CHANGELOG
- Focus on changes to the
findOneAnd*method - Check for other breaking changes
Official Source:
Step 2: Local Test ✅
Step 3: Check the log output ✅
Step 4: Fix compatibility issues ✅
If the test fails:
-
Positioning problem:
-
Analysis error:
- Is the format of the return value changed?
- Is it a new/deleted field?
- Is it a logical change in behavior?
-
Modify the encapsulation layer or matrix:
- Check
src/adapters/mongodb/writes/andsrc/adapters/mongodb/common/first - Keep the public API unchanged
- If only the verification range changes, update
test/compatibility/matrix.jsonfirst
- Check
-
Update documentation:
- Updated "Supported driver versions" of this document
- Update CHANGELOG.md
- Updated compatibility notes in API documentation
Step 5: Regression Testing ✅
Fix example: How to adapt driver 7.x (assumed)
Assume that MongoDB driver 7.x changes the return value format again:
Scenario: Driver 7.x returns { document, metadata } format
Key Points:
- First use temporary driver coverage to verify public API behavior.
- If a breaking change is found, priority is given to repairing it on the thin encapsulation layer of
src/adapters/mongodb/. - The public API remains unchanged and user code does not need to be modified.
Test Strategy
Test coverage
Key test scenarios
Required test scenarios:
- Find the document and modify it
- Document not found (returns null)
- upsert inserts a new document
- Return to the document before update (
returnDocument: "before") - Return to the updated document (
returnDocument: "after") - Contains complete metadata (
includeResultMetadata: true) - Cache automatically expires
- Concurrency safety
Automated test commands
Developer Guide
Add new findOneAnd* style methods
If you need to add a similar method in the future (such as a custom findOneAndModify), please follow this pattern:
Key Points:
- Keep TypeScript types consistent with
Collection<TSchema>method signature. - No new hidden version detection logic is added; version differences are discovered using matrix testing.
includeResultMetadata: trueis explicitly passed in by the caller when metadata is required.- Synchronously update
test/compatibility/and verification documents after modification.
Related resources
Internal documentation
-
Verification Entry:
test/validation/VERIFICATION-PROGRESS.md- Current verification progresstest/validation/REAL-SERVER-MATRIX.md- Real service matrix resultstest/compatibility/matrix.json- Driver/server version matrix configuration
-
API Documentation:
docs/find-one-and-update.md- Contains compatibility notesdocs/find-one-and-replace.md- Contains compatibility notesdocs/find-one-and-delete.md- Contains compatibility notes
External resources
FAQ
Q1: Will there be any problems if I am using MongoDB driver 5.x?
A: The current dependency baseline does not resolve to Driver 5.x. If the application is forcibly replaced with 5.x through package manager overrides, please restore the package dependency baseline first:
If you must use 5.x, run compatibility verification yourself and handle differences between { value, ok, lastErrorObject } and document objects at the application layer.
Q2: How to know the driver version currently used?
A: Check for package-lock.json or run:
or in code:
Q3: What should I do if the test fails after upgrading to a future driver major version?
A: Follow the "Future Driver Upgrade Guide":
- Look at the failed test suite (specifically
findOneAnd*) - Analyze error logs
- Evaluate whether compatibility processing is required for the corresponding thin packaging layer of
src/adapters/mongodb/ - Keep the public API unchanged
If you need help, please attach test:compatibility and server matrix output to submit an Issue.
Q4: Why are only the findOneAnd* methods affected?
A: Because the return value format of these methods is more complex (including metadata), while the return value format of other methods (such as updateOne) is simple and unchanged.
Q5: Will multiple driver versions be supported in the future?
A: There are currently no plans to support multiple versions. Reason:
- Increased maintenance costs
- Increase test complexity
- MongoDB driver follows semantic versioning, and the differences between major versions are clear
Recommended practice: Upgrade monSQLize with the major version upgrade of the MongoDB driver.