Author: Niccolò Caselli Date: 2025-07-30
A Node.js utility tool that migrates package names in MongoDB documents' _class
fields used by Spring Data MongoDB for JSON deserialization during ORM operations.
When you refactor your Java package structure in Spring Boot applications, existing MongoDB documents still contain the old package names in their _class
field. This causes deserialization failures when Spring Data MongoDB tries to instantiate objects from the database.
For example, if you change your package from org.caselli.cognitiveworkflow
to org.stdlab.sallma
, existing documents in MongoDB will still have:
{
"_id": "...",
"_class": "org.caselli.cognitiveworkflow.User",
"name": "John Doe"
}
This will cause Spring Data MongoDB to fail when trying to deserialize the document because it can't find the class org.caselli.cognitiveworkflow.User
.
This tool automatically updates all _class
fields in your MongoDB collections to use the new package prefix, ensuring seamless migration when you refactor your Java package structure.
- Node.js (version 14 or higher)
- MongoDB instance (local or remote)
- Access to the MongoDB database you want to refactor
- Clone or download this repository
- Install dependencies:
npm install
node index.js <mongodb-uri> <old-package-prefix> <new-package-prefix>
mongodb-uri
: The MongoDB connection stringold-package-prefix
: The current package prefix in your documentsnew-package-prefix
: The new package prefix you want to use
node index.js "mongodb+srv://username:password@cluster.mongodb.net/mydb" "com.oldcompany.app" "com.newcompany.app"
- Connection: Connects to the specified MongoDB instance
- Collection Discovery: Scans all collections in the database
- Document Search: For each collection, finds documents with
_class
fields matching the old package prefix - Package Refactoring: Updates each document's
_class
field to use the new package prefix - Reporting: Provides detailed logging of the refactoring process
Spring Data MongoDB Package Refactoring Parameters:
MongoDB URI: mongodb://localhost:27017/mydb
Old Java Package: org.caselli.cognitiveworkflow
New Java Package: org.stdlab.sallma
Target: _class fields used by Spring Data MongoDB for JSON deserialization
Connecting to MongoDB for Spring Data package refactoring...
Connected successfully
Found 3 collections
Scanning collection for Spring Data _class fields: users
Found 5 Spring Data documents to refactor
SUCCESS: Refactored _class: org.caselli.cognitiveworkflow.User → org.stdlab.sallma.User
SUCCESS: Refactored _class: org.caselli.cognitiveworkflow.User → org.stdlab.sallma.User
SUCCESS: Refactored _class: org.caselli.cognitiveworkflow.User → org.stdlab.sallma.User
SUCCESS: Refactored _class: org.caselli.cognitiveworkflow.User → org.stdlab.sallma.User
SUCCESS: Refactored _class: org.caselli.cognitiveworkflow.User → org.stdlab.sallma.User
Scanning collection for Spring Data _class fields: orders
SKIP: No Spring Data documents found with old package prefix
Scanning collection for Spring Data _class fields: products
Found 2 Spring Data documents to refactor
SUCCESS: Refactored _class: org.caselli.cognitiveworkflow.Product → org.stdlab.sallma.Product
SUCCESS: Refactored _class: org.caselli.cognitiveworkflow.Product → org.stdlab.sallma.Product
Spring Data package refactoring completed successfully!
Total Spring Data documents refactored: 7
Connection closed
{
"_id": ObjectId("..."),
"_class": "org.caselli.cognitiveworkflow.User",
"name": "John Doe",
"email": "john@example.com"
}
{
"_id": ObjectId("..."),
"_class": "org.stdlab.sallma.User",
"name": "John Doe",
"email": "john@example.com"
}
- Backup First: Always backup your MongoDB database before running this tool
- Test Environment: Test the tool on a copy of your production data first
- Package Structure: Ensure your new package structure is already implemented in your Java code
- Spring Data Classes: Make sure your Spring Data entity classes are updated to use the new package names
- Connection Failed: Check your MongoDB URI and network connectivity
- No Documents Found: Verify the old package prefix is correct
- Permission Denied: Ensure your MongoDB user has read/write permissions
- Invalid URI: Make sure the MongoDB URI follows the correct format
ERROR: Invalid MongoDB URI format
- Check your connection stringERROR: Package prefixes cannot be empty
- Provide both old and new package prefixesERROR: Failed to update Spring Data document
- Check database permissions
Feel free to submit issues and enhancement requests!
This project is open source and available under the MIT License.
If you encounter any issues or have questions, please open an issue in the repository.