ObjectId Auto Conversion
Overview
monSQLize can convert valid 24-character hexadecimal strings to MongoDB ObjectId instances before an operation is executed. This lets application code pass request IDs as strings in normal query and write paths.
Key benefits:
- Simplifies application code: no manual
new ObjectId()calls for common fields. - Improves developer ergonomics: string IDs can be passed directly.
- Detects valid ObjectId strings automatically.
- Handles nested objects and arrays.
- Preserves legacy behavior where ObjectId-looking values are normalized automatically.
Why Automatic Conversion Helps
Manual conversion
Automatic conversion
Conversion Rules
Automatic Detection Conditions
monSQLize converts a string to ObjectId only when it satisfies all of these conditions:
- It is exactly 24 characters long.
- It contains only hexadecimal characters (
0-9,a-f,A-F). - MongoDB accepts it as a valid
ObjectId. - It is not a MongoDB field reference such as
"$userId".
Current stable behavior is value-based, not field-whitelist based. Internal helper patterns such as _id and *Id exist for compatibility with nested object handling, but they do not restrict conversion of valid bare string values. If traversal reaches a valid ObjectId-looking string, it can be converted regardless of the field name.
The converter also skips MongoDB expression operators such as $expr, $function, $where, and $accumulator to avoid changing executable expressions.
Examples
Configuration Options
Enable or Disable Automatic Conversion
Option Reference
maxDepth is a traversal safety cap. If a very deep $and / $or tree or nested document contains ObjectId-looking strings beyond that depth, monSQLize leaves those strings as-is. Increase maxDepth for intentionally deep query shapes that still need automatic conversion.
excludeFields and { field: false } use the same matcher on query and write paths. A value such as b matches b, a.b, and nested array paths such as a.b[0].c; wildcard patterns still match the normalized dot path.
Usage Examples
Basic Queries
Complex Filters
Update Operations
Delete Operations
Supported Methods
ObjectId auto conversion is applied by these methods.
For ordinary ID lookups, prefer the same query shape you would use in MongoDB: findOne({ _id }) for one document and find({ _id: { $in: ids } }) for multiple documents. The convenience helpers findOneById(id) and findByIds(ids) remain available as API reference methods, but they are not required to benefit from automatic conversion.
Query Methods
find(query)findOne(query)findOneById(id)findByIds(ids)findPage(options)findAndCount(query)count(query)distinct(field, query)
Write Methods
insertOne(doc)insertMany(docs)updateOne(query, update)updateMany(query, update)replaceOne(query, doc)upsertOne(query, update)deleteOne(query)deleteMany(query)
Batch Methods
insertBatch(docs)updateBatch(query, update)deleteBatch(query)
Other Methods
aggregate(pipeline), including stages such as$matchand$lookupfindOneAndUpdate(query, update)findOneAndDelete(query)findOneAndReplace(query, doc)
Advanced Configuration
Current Stable Controls
The default behavior is value-based conversion: any valid 24-character hex string can be converted when traversal reaches it. You can keep the default and add escape hatches for fields that must remain strings:
Use autoConvertObjectId: false or { enabled: false } for collections or code paths that must preserve every 24-character hex string. Use excludeFields or { fieldName: false } when only specific business fields such as transaction hashes, idempotency keys, signatures, or external payment numbers must stay as strings.
Handling Mixed String and ObjectId Identifiers
When a schema mixes ObjectId fields and business strings that can look like ObjectIds, prefer one of these approaches:
- Store those business values in a MongoDB type that cannot be confused with ObjectId values.
- Disable automatic conversion for that monSQLize instance, or exclude only the business fields that must remain strings.
- Use the underlying MongoDB collection directly for specialized paths that must preserve every string exactly.
Scope Notes
Automatic conversion applies to query filters, insert documents, replace documents, common update operator payloads, delete filters, and aggregation pipelines. Update pipelines are left unchanged.
Performance Notes
ObjectId auto conversion has a very small overhead:
Performance Impact
- Query filter conversion: usually less than 1 ms per query.
- Single document conversion: usually less than 1 ms.
- Batch operation conversion: about 0.1 ms per document.
Optimization Tips
Optimization tips:
-
Avoid very deep nesting.
- Keep common query/update structures at five levels or fewer.
- Consider flattening data that requires deeper traversal.
-
Disable automatic conversion for workloads that must preserve arbitrary 24-character hex strings.
- Convert true ObjectId fields manually in those paths.
- Keep mixed identifier fields out of automatic conversion paths.
-
Prefer batch methods when appropriate.
- Use
insertBatchinstead of repeatedinsertOnecalls. - Batch conversion is more efficient than repeated single-operation setup.
- Use
FAQ
Q1: How do I disable automatic conversion?
You can also use { enabled: false }. Configure this at instance creation time. This setting now applies consistently across query filters, insert/replace documents, common update operator payloads, delete filters, and aggregation pipelines.
Q2: How should I handle fields with mixed identifier types?
Some fields can contain either ObjectId values or regular strings.
Best practice:
- Avoid mixed identifier types in the data model when possible.
- If mixed types are unavoidable, disable automatic conversion for that instance or exclude the string-only fields with
excludeFields/{ field: false }. - Normalize identifier formats at the application boundary.
Q3: Are excludeFields or field-map escape hatches supported?
Yes. Conversion remains value-based by default, but excludeFields, { fieldName: false }, and maxDepth are supported escape hatches for string-only business fields.
Q4: Does automatic conversion affect query performance?
No meaningful MongoDB query-performance impact is expected. Conversion happens before the query is sent to MongoDB and usually adds only about 0.1-1 ms of local processing.
Q5: How can I confirm that a field was converted?
Use integration tests or MongoDB command monitoring to inspect the value that reaches the driver. The current converter does not emit per-field conversion logs.
Q6: Are ObjectId strings inside arrays converted?
Yes. This includes arrays used by operators such as $in and $nin.