Configuration & Customization

This guide covers driver settings, Read/Write concerns, handling unimplemented features, and tracking overall database fidelity in Mongomock.

BSON Codec Options

Mongomock parses PyMongo CodecOptions. However, only a subset of these options are supported at this time:

  • Timezone Awareness (tz_aware): Fully supported. When enabled, retrieved datetime fields are populated with UTC timezone info (bson.tz_util.utc).
  • UUID Representation: Standard conversion behaviors are maintained.
  • Unsupported Options: Custom type registries, decode error handlers, and custom document classes are not currently supported and will raise a NotImplementedError if attempted.

Read Preferences, Read Concerns, & Write Concerns

Mongomock allows setting standard Read Preferences, Read Concerns, and Write Concerns to maintain compatibility with PyMongo signatures, but they are behaviorally simplified:

  • Write Concern (WriteConcern): Write operations are always treated as acknowledged and executed synchronously in memory. Write concerns are parsed to verify signature compatibility but do not affect execution.
  • Read Concern (ReadConcern): Read concerns are parsed for correctness but do not affect transaction isolation levels in Mongomock's single-node in-memory store.
  • Read Preference (ReadPreference): Read preferences (like PRIMARY or NEAREST) are validated but have no effect since Mongomock operates on a single logical node representation.

Managing Unimplemented Features

MongoDB is a feature-rich database, and Mongomock does not support its entire query surface. When Mongomock encounters an unsupported stage or query operator, it raises a NotImplementedError to prevent tests from passing falsely.

If you prefer to ignore specific unimplemented options during migrations or feature testing, you can mute them using ignore_feature:

import mongomock

# Mute NotImplementedErrors for collation and logical session validations
mongomock.ignore_feature("collation")
mongomock.ignore_feature("session")

To restore standard strict exception behaviors:

mongomock.warn_on_feature("session")

Known Limitations & Missing Features

Below is a list of features that are currently missing in Mongomock. Pull requests adding these features are highly appreciated.

  • Complex $rename: Dot-notation and complex nested renames are not supported.
  • Query Operations: $jsonSchema, $text search, $where execution.
  • Custom Collection Options: Advanced create_collection configurations, codec overrides, and document validation structures.
  • Raw BSON Batches: aggregate_raw_batches and find_raw_batches are not implemented.
  • Miscellaneous Pipeline Operators: Set operators ($setIntersection, $setDifference, ...), specialized date operators ($isoDayOfWeek, $isoWeekYear, ...), and specific bucket aggregation behaviors.

To help implement these missing features or run local tests, head over to the Contributing Guide.