Migration Guide: Unified ID Field
Migration Guide: Unified ID Field
This guide helps you migrate from the legacy _id field to the unified id field API.
What Changed?
ObjectQL now provides a unified id field across all database drivers:
- Before: MongoDB used
_id, SQL usedid- inconsistent API - After: Both drivers use
idat the API level - consistent API
The MongoDB driver now automatically maps between id (API) and _id (database) internally.
Benefits
✅ Consistent API: Write database-agnostic code
✅ Easier Migration: Switch between MongoDB and SQL without code changes
✅ Better Developer Experience: No need to remember which driver uses which field
✅ Backward Compatible: Legacy _id usage still works (MongoDB only)
Migration Steps
1. Update Filters
Before:
After:
2. Update Document Creation
Before:
After:
3. Update Field Access in Results
Before:
After:
4. Update Sorting
Before:
After:
5. Update Field Projection
Before:
After:
Automated Migration
You can use a find-and-replace tool to migrate your codebase:
Search Patterns
- In Filters:
['_id',→['id', - In Objects:
_id:→id: - In Access:
.\_id→.id - In Fields:
'_id'→'id'
Example Script
⚠️ Warning: Always review changes manually and test thoroughly after automated replacement.
Backward Compatibility
MongoDB Driver
The MongoDB driver maintains full backward compatibility for _id usage:
_idin filters is automatically mapped to MongoDB's_id_idin sorting is automatically mapped to MongoDB's_id_idin field projections is automatically mapped to MongoDB's_id_idin create operations is mapped toid- Results always return
id(not_id)
Example:
No Breaking Changes: Your existing queries using _id will continue to work without modification. However, migrating to id is recommended for:
- Consistency with SQL drivers
- Database portability
- Future-proofing your code
SQL Driver
The SQL driver has always used id, so no migration needed for SQL-only codebases.
For codebases using MongoDB-style _id with SQL:
Testing Your Migration
After migrating, verify the following:
1. Create Operations
2. Find Operations
3. Update Operations
4. Sort and Projection
Database Migration
MongoDB Collections
No database migration needed! The MongoDB driver:
- Continues to use
_idin the database (MongoDB requirement) - Automatically maps
id↔_idat the driver level - Existing data works without changes
SQL Tables
If you have legacy SQL tables using _id as the primary key:
Common Issues
Issue: undefined for id field
Cause: Using old code that accesses _id on results
Solution: Change result._id to result.id
Issue: Filters not working
Cause: Still using _id in filter arrays
Solution: Change ['_id', '=', value] to ['id', '=', value]
Issue: Custom ID not working in MongoDB
Cause: Passing _id instead of id in create
Solution: Change { _id: 'custom' } to { id: 'custom' }
Need Help?
If you encounter issues during migration:
- Check the Driver Documentation
- Review the API Reference
- Open an issue on GitHub
Summary
| Aspect | Before (Legacy) | After (Unified) |
|---|---|---|
| MongoDB Filter | ['_id', '=', '123'] | ['id', '=', '123'] |
| SQL Filter | ['id', '=', '123'] | ['id', '=', '123'] |
| MongoDB Create | { _id: '123', ... } | { id: '123', ... } |
| SQL Create | { id: '123', ... } | { id: '123', ... } |
| MongoDB Result | result._id | result.id |
| SQL Result | result.id | result.id |
Recommendation: Migrate all code to use id for maximum consistency and portability.