Seeking Efficient Method for Syncing MongoDB ObjectId Data into Realm Without Manual Conversion

Hello ,

I am currently working on synchronizing data from our backend API into a Realm database. Our backend, which utilizes MongoDB, provides data in Extended JSON format, with ObjectId fields represented as:

{
  "_id": "63c16cac9041014587115a38",
  // other fields
}

Main Concern:

I aim to avoid manually iterating over each API response to convert the _id fields from their string representation into Realm.BSON.ObjectId instances before inserting them into the Realm database. This manual process becomes increasingly complex with nested structures and large datasets.

Questions:

  1. Is there a way for Realm to automatically recognize and convert these $oid representations into its Realm.BSON.ObjectId type during data synchronization?

  2. Can we configure Realm to understand that fields like _id are of type ObjectId based on the schema, thereby facilitating automatic conversion?

  3. Are there best practices or recommended approaches for handling such scenarios to maintain data consistency and reduce manual processing?

do you have any recommendations or efficient methods for manually syncing data from the server to the client, especially concerning the conversion and handling of ObjectId fields?

Additional Context:

I am aware that MongoDB has announced the deprecation of Atlas Device Sync, with support ending on September 30, 2025.

Any guidance or suggestions on achieving seamless integration of existing ObjectId values into Realm, considering our sync setup and the upcoming deprecation, would be greatly appreciated.

Thank you for your assistance.

Best regards,
Rajan Shah

Hi @Rajan_Shah1,

That seems odd: if the _id is an ObjectId in MongoDB, the Extended JSON representation should look like:

{
  "_id": {"$oid": "63c16cac9041014587115a38"},
  // other fields
}

Are you sure you’re not getting your data in regular JSON format instead?

Nevertheless:

  • the Realm SDK has no issue working with either representation, as long as your backend schema matches the client app’s (i.e. a String matches string, ObjectId matches objectId, …).
  • there should then be no need to convert anything: as documented, Realm supports more types than just ObjectId, and you can get the proper classes via the Realm Object Models feature in the Atlas UI.

_id can be of type string , int , or objectId .

Could you please elaborate on why you think a conversion is needed?

Question:

Just to confirm — if I receive data from my API in this Extended JSON format:

{
  "_id": { "$oid": "63c16cac9041014587115a38" },
  // other fields
}

And my Realm schema defines _id as objectId, will Realm automatically interpret this as a valid Realm.BSON.ObjectId, without requiring any manual conversion on the client side?

Question:

Can Realm directly parse and work with MongoDB’s Extended JSON format without requiring manual conversion on the client side — especially for the following data types?

  • ObjectId (e.g., "_id": { "$oid": "..." })
  • Date (e.g., "updated_at": { "$date": { "$numberLong": "..." } })
  • Int32 / Int64 (e.g., "$numberInt": "123", "$numberLong": "456")
  • Decimal128 (e.g., "$numberDecimal": "12.34")
  • Double (e.g., "$numberDouble": "45.67")
  • Boolean (e.g., true/false)
  • UUID (e.g., { "$uuid": "..." })
  • Binary (e.g., { "$binary": { "base64": "...", "subType": "00" } })

My goal is to avoid iterating over the API response to manually convert these into Realm.BSON types before inserting into the Realm database. I want to ensure that if the Realm schema defines the correct types (e.g., objectId, date, int, etc.), Realm will automatically handle these formats.


Hi @Rajan_Shah1,

Device Sync interfaces directly with MongoDB collections, the SDK talks to a specific backend service that is fully transparent for the user, and exposes to Realm all the supported fields and their types, can you please explain why you think is necessary to go through an API, and which one are you referring to?

It looks like we’re missing something here, probably giving a description of your setup, instead of single questions, may help understanding the whole context.

Thank you for your response.

You’re right — let me clarify the full context.

Since Atlas Device Sync is deprecated, I’m building a custom sync solution because our use case doesn’t require real-time/live sync. Instead, we only need to synchronize data periodically (on-demand) — for example, during login or on a fixed schedule.

So, I’m syncing data from our backend using REST APIs that return data in MongoDB Extended JSON format. My goal is to insert that data into the local Realm database without manually converting each field (like _id, date, etc.) to Realm-specific BSON types on the client side.

At this stage, I cannot rely on third-party sync services like PowerSync or Ditto due to cost, control, and offline-first behavior concerns.

So, I’d really appreciate your guidance:

  • Is using custom APIs a reasonable approach for one-way sync scenarios like ours?
  • Do you recommend any best practices when syncing data to Realm manually (e.g., schema consistency, type handling)?
  • Can Realm SDK (Node/React Native) parse and store Extended JSON types like ObjectId, Date, Decimal128, etc., directly — assuming the schema matches?

Any suggestions you can share will be very helpful as we move forward with a reliable, controlled sync layer.

Thank you!

Best regards,
Rajan Shah


Hi @Rajan_Shah1,

Thank you for the additional information: yes it helps understanding what you’re doing.

You shouldn’t have any problem using the BSON package, then, as long as the type can be mapped between the two

It may well be. You could also try to mix your approach with Parse, using their tested (and open source) server, and handle the local database needs with Realm: that, however, would require some type mapping.

The hardest thing Device Sync ever had to do was conflict resolution: if the same object is modified twice in a short interval, what’s the correct way to apply changes? If your setup is meant to be a sort of online backup, for single users, then you likely don’t have to worry much about it, but most other use cases require some sort of conflict resolution, so keep that in mind.

Yes, overall, even though there are minor differences (Realm doesn’t really have a int type, all integers are long, for example).

While the Generate SDK Object Models functionality is still available, take advantage of it to understand in detail how MongoDB types translate to Realm types.

1 Like