With the deprecation of Atlas Device Sync, Ditto is the recommended replacement for synchronizing edge devices with MongoDB. Refer to our Migration Guide for Atlas Device Sync for a comprehensive guide on moving from Atlas Device Sync to Ditto.
The MongoDB Connector is now generally available and is available to all paying Ditto customers.If you are interested in using the MongoDB connector, please visit the MongoDB Connector page, which has further details around requesting access.
How It Works
The MongoDB Connector is provided as a managed service, running alongside the Ditto Server that you are using for your Ditto applications. The Ditto Server is a centralized cloud datastore and synchronization engine that is analogous to the Atlas Device Sync service.
Data Modelling Considerations
Both Ditto and MongoDB store their data in the form of JSON-like documents, so are a natural fit together when building applications that run on the edge. Unlike integrations with other systems that rely on synchronizing to relational forms (e.g. sqlite), you do not need to remodel your data to use Ditto and MongoDB together. However, there are a few considerations that you need to make when using Ditto.ID Mapping Between Systems
Due to security considerations in a peer-to-peer context, Ditto’s permission system (see Data Authorization) only has access to the unique and immutable ID field (_id
). To use permissions effectively, most Ditto applications use objects rather than strings as their _id
. These sub-fields with the _id
are then often duplicated in the document’s body to facilitate POJO/DTO-like patterns in your application code. For example you may choose to model an order (in a retail system) as follows:
JSON
ObjectIds
, essentially UUIDs with a time-based component; alternatively you may choose to use a single field value as the ID (e.g. orderId
). It’s very rare for IDs in MongoDB to be used in the same way as within Ditto, therefore IDs likely need to be handled differently in both systems.
The MongoDB Connector automatically bridges this difference in IDs, so that both systems can converge, even if their primary keys differ. It does this through the “ID Mapping” process, based on the configuration that you provide to the connector.
Fields used in the id mapping must be immutable and always present to achieve convergence between the two systems. If these requirements are not met, it will result in undefined behavior, such as duplicate Ditto documents with different IDs, or failure to synchronize the document between MongoDB and Ditto.
- 1:1 ID (i.e. the same ID is used in both Ditto and MongoDB)
- Single Document Field
- Multiple Document Fields
1:1 ID
This mode is used if you select the “Match IDs” option, which will use the_id
field in the ID mapping.
In that case the ID fields used between Ditto and MongoDB will be identical.
However, some restrictions apply here:
NULL
cannot be present in the ID- Arrays cannot be present in the ID
- The ID cannot be an object
Single Document Field
This mode is used if only a single field is specified in the field mapping.You cannot use the
_id
field itself in this mode.
You must only refer to top-level fields in the document, nested fields are not supported.orderId
field and the following MongoDB document:
MongoDB
JSON
Multiple Document Fields
When the ID mapping contains multiple fields, like the restaurants collection in the example above, the Ditto ID will become an object with the specified fields and corresponding values from the document. Note that the Id Mapping cannot refer to the_id
field itself in this case.
You cannot use the
_id
field itself in this mode.
You must only refer to top-level fields in the document, nested fields are not supported.location
and orderId
fields and the following MongoDB document:
MongoDB
Ditto Document
MongoDB Datatypes
MongoDB stores data as BSON, which includes rich datatypes such as nativedate
objects, you can read more about the datatypes available in https://www.mongodb.com/docs/manual/reference/bson-types/.
Ditto represents its data using JSON with a few key additional datatypes (e.g. integer), which does not have the full set of datatypes available in BSON. The connector will perform automatic mapping of BSON datatypes to JSON when synchronizing data from MongoDB to Ditto.
How data is mapped between JSON and BSON is dependent on whether you are using EJSON mode or not.
EJSON mode can be configured on a collection-by-collection basis.
EJSON Mode
If EJSON mode is enabled, all BSON data stored in MongoDB will be mapped to its canonical JSON representation prior to being stored in Ditto. Similarly, all data stored in Ditto is expected to be in EJSON canonical form and will be mapped to its BSON representation prior to being stored in MongoDB. This ensures that any BSON data stored in MongoDB is correctly represented in Ditto and vice versa, no matter where the data is created or modified. For example, if you have the following MongoDB BSON document:Ditto Document
As EJSON canonical form is a significantly more verbose representation of the data, you will have to write your code and queries within Ditto to expect this more complex representation. See our comprehensive guide on Working with EJSON for detailed information on querying EJSON data, performance considerations, and platform-specific code examples.
Native BSON Mapping
If EJSON mode is not enabled, the connector will perform a native mapping of BSON datatypes to JSON. This is equivalent to the EJSON relaxed mode, where any BSON datatype that is not explicitly defined in the collection definition will be mapped to a JSON object. The full set of mappings are listed below:MongoDB BSON Datatype | Ditto JSON Datatype |
---|---|
Double | number |
String | string |
Object | object |
Array | array |
ObjectId | string |
Boolean | boolean |
Date | string (ISO8601 format) |
Null | null |
Regular Expression | object (see EJSON v2) |
32-bit Integer | integer |
64-bit Integer | integer |
Timestamp | number (epoch timestamp) |
Decimal128 | string |
All BSON datatypes that are not listed above will not be replicated between Ditto and MongoDB.
Setting Up the Connector
To setup the connector, you first must carry out some steps within your MongoDB cluster, then configure the connector via API to launch the connector.Pre-requisites
You need to have a MongoDB cluster already setup, the minimum supported version for the connector is currently MongoDB 7.0.13 and MongoDB 8.0.0. You need to follow a number of steps to ensure that MongoDB is ready to be used with the connector.Create MongoDB Database
If your target database doesn’t currently exist, then you must first create this within MongoDB. No special configuration is required for the created database. An existing database can be used if one exists.Create MongoDB Collections
All collections you wish to synchronize with Ditto must exist within MongoDB before setting up the connector. Each of these synchronized collections must havechangeStreamPreAndPostImages
set to true
. This setting allows the connector to ensure causal consistency between both systems, and the connector will not start if this is not set for all collections.
When creating new collections, pass changeStreamPreAndPostImages: true
to your creation command. For pre-existing collections, you can modify this configuration using collMod
.
See Enable Change Stream Pre and Post Images.
For example, to edit an existing collection called orders
, you can run the following command:
Create a MongoDB Database User
The connector authenticates to MongoDB using the username and password of a database user. We recommend creating a dedicated database user for the connector to use within MongoDB. At a minimum the database user will require thereadWrite
permission for the specific database that you’re looking to synchronize with Ditto. See Configure MongoDB Database Users. If you are connecting to a sharded database (i.e. a mongos
connection) the database user also needs to have the explicit read
permission for the built-in collection
collection in the config
database, so it can inspect sharding configurations.
Add Ditto IPs to MongoDB Allowlist
The Ditto Server uses the following 3 IPs for interacting with external services such as MongoDB:Configuring the Connector
To get access to the MongoDB Connector UI, you’ll need to contact Ditto to have your organization enrolled.
Settings
> MongoDB Connector
.

Step-by-Step Guide
1
Fill in the database field, with the MongoDB database that you wish to synchronize with Ditto.
2
Fill in the connection string field, with the connection string of the MongoDB database that you wish to synchronize with Ditto.
You can find this connection string in the MongoDB Atlas UI, under the
Connect
button for your cluster, ensuring that you use the credentials of the database user that you created in the prerequisites.The connection string must be supplied in the format mongodb+srv://<username>:<password>@<cluster>.mongodb.net/
.
For example to connect to the cluster with the URL my_cluster.mongodb.net
, using the username my_user
with a password of my_password
, you would provide mongodb+srv://my_user:my_password@my_cluster.mongodb.net/
.
Non-SRV-based connection strings cannot be used with the MongoDB connector.While the password is passed into the Ditto Portal as part of the connection string, it is stored securely in Ditto’s systems and is not accessible to Ditto staff.
3
Fill in the ID mapping field, with the ID mapping that you wish to use for the connector.You can add new collections to the connector by clicking the 
If you select the 
Once done, click the
Add Collection
button, entering the collection name, toggling initial sync, then selecting how you would like your IDs mapped.Enabling initial sync will trigger an initial sync of the collection from MongoDB to Ditto, this is rate limited and will be done in the background to avoid affecting the performance of the connector. Once the initial sync is complete, the connector will continue to synchronize the collection on an ongoing basis via MongoDB Change Streams.For very large collections, you may opt to not rely on the Connector to perform the initial sync, and instead perform the initial sync manually via the Ditto API so that you have finer-grained control over the process.If you select the Match IDs
option, then the ID fields used between Ditto and MongoDB will be identical.
Map fields to Ditto ID
option, then you will need to add all of the fields that you wish to map to the Ditto ID.
Add collection
button to add the collection to the connector configuration, repeat this step for each collection you wish to synchronize with Ditto.ID Mapping is a critical part of configuring the connector, please makes sure you read the ID Mapping section carefully and configure as necessary for your use case.
4
Once all of the fields have been added, click the
Save
button to create the connector.5
Once the connector has been created, you will be able to see the status of the connector in the Ditto Portal.
It will first show as
Pending
while the connector is being created, then once it is ready it will show as Running
.
In case of an error, the connector will show as Failed
, and you will be able to see the error message in the Ditto Portal, see also Troubleshooting Connectivity.Troubleshooting Connectivity
When the connector starts up, it will perform a number of checks to ensure that it is able to connect to MongoDB and that the configuration is correct. If any of these checks fail, the connector will show asFailed
, and you will be able to see a more detailed error message in the Ditto Portal.
These checks are as follows:
- Ensure that the MongoDB connection string is reachable
- Ensure that the MongoDB database user has the correct permissions
- Ensure that the MongoDB cluster has the correct IP addresses added to the allowlist
- Ensure that the MongoDB database exists
- Ensure that the MongoDB collections exist
- Ensure that the MongoDB collections have
changeStreamPreAndPostImages
set totrue
- Ensure that it can create all of the necessary metadata collections
Error Message | Remediation |
---|---|
Connection string provided is invalid. | Delete the connector and try again, ensuring that the connection string is correct. |
Failed to connect to MongoDB | • Ensure that the MongoDB cluster has the correct IP addresses added to the allowlist (see Add Ditto IPs to MongoDB Allowlist) • Ensure that the MongoDB database user exists and has the correct permissions (see Create a MongoDB Database User) |
Failed to list database names | Ensure that the MongoDB database user has the correct permissions (see Create a MongoDB Database User) |
Failed to list collections | Ensure that the MongoDB database user has the correct permissions (see Create a MongoDB Database User) |
Database <name> not found | Ensure that the MongoDB database exists (see Create MongoDB Database) |
Collection <name> not found | Ensure that the MongoDB collection exists (see Create MongoDB Collections) |
Collection <name> must be configured with changeStreamPreAndPostImages enabled | Ensure that the MongoDB collection has changeStreamPreAndPostImages set to true (see Create MongoDB Collections) |
Collection <name> cannot be a capped collection | Ensure that the MongoDB collection is not a capped collection (see Create MongoDB Collections) |
Collection <name> cannot be created | Ensure that the MongoDB database user has the correct permissions (see Create a MongoDB Database User) |
Something went wrong | An unknown error occurred, please contact Ditto support for assistance. |
Reconfiguring the Connector
There are some cases where you may need to re-configure the connector, for example:- You need to add a new collection to the connector
- You need to remove a collection from the connector
- You need to change the ID mapping for a collection
- You need to change the credentials used by the connector
Edit
button on the MongoDB Connector page in the Ditto Portal.
This will open a form with the current configuration, allowing you to modify the configuration as required:

Save
button to update the connector with the new configuration.
Behavior on Re-configuration
When you re-configure the connector, the connector will automatically take specific actions based on the changes you have made:- If adding a new collection, the connector will automatically trigger an initial sync of the collection from MongoDB to Ditto, if initial sync is enabled in the collection configuration.
- If changing the ID mapping for a collection, the connector will automatically trigger an initial sync of the collection from MongoDB to Ditto if enabled in the collection configuration. All documents imported during this initial sync will be using the new ID mapping, but existing documents in Ditto will not be updated, you will need to manually delete these old documents in Ditto if desired.
- If toggling initial sync on a collection, the connector will not automatically trigger an initial sync of the collection from MongoDB to Ditto, unless there are other changes to the collection configuration (e.g. ID mapping).
- If removing a collection, the connector will stop synchronizing the collection between MongoDB and Ditto. Existing documents in this Ditto collection will not be deleted you will need to manually delete these documents in Ditto if desired.
- If changing the credentials used by the connector, the connector will automatically reconnect to MongoDB using the new credentials. No document changes will be missed as part of this rotation process.
Internal Metadata
In order to provide consistency across the two systems, the connector uses some internal metadata. Specifically this metadata tracks:- Current MongoDB sessions, to avoid circular writes between the two systems. This is stored in the
__ditto_connector_sessions
internal collection within your MongoDB database. These are periodically cleaned up by the connector when the sessions are no longer needed. - Documents that failed to synchronize from Ditto to MongoDB (for example they may have been rejected due to schema validation issues). These are stored in the
__ditto_unsynced_documents
internal collection within your MongoDB database. These documents serve as a record for you to know which documents have failed to synchronize, and can be used to manually resync the documents. This collection is not automatically cleaned up by the connector, you can choose to delete the documents in this collection if you no longer need to keep track of these documents. - BSON field mappings for documents, currently stored as a metadata field (prefixed with
_
within the Ditto document)