user2901933
user2901933

Reputation: 149

Converting Parse objectId to Mongo ObjectId?

I'm trying to migrate data from Parse to a new project that uses Mongo as its database (without Parse/Parse Server). Since the schemas are different between the two projects, I'm manually writing a migration script to achieve this.

As I understand it, Parse appears to use 10-character-long IDs for their objects (combinations of digits, lower-case letters, and upper-case letters), while Mongo uses 24-character-long IDs (12 bytes represented as hex).

Right now, when migrating data for a document from the old project to the new one, I'm using a function that converts the Parse ID to a unique Mongo ObjectId (it converts each character to a 2-digit hex value, then pads the 20-character string with 4 zeroes).

Is this a good approach? I'm avoiding using Mongo's automatic ObjectId generation in case I ever need to re-migrate any of the old Parse documents and find the matching document in the new database. I know automatically generated ObjectIds in Mongo also embed some other information like creation dates, but I don't think this would be important and I can just use my custom ObjectId generator? However, I'm not sure about the implications for performance/if I'm just going about this migration the wrong way.

Upvotes: 2

Views: 2859

Answers (2)

user6565893
user6565893

Reputation:

The approach i recommend is letting Mongo auto-generate the ids and then storing Parse's ids in a new field called parseID for future reference if needed.

For example:

PARSE DATA:

   "_id": ObjectId(1234567890),
   "title": "Mongo Migrate", 
   "description": "Migrating from Parse to Mongo"



MONGO DATA:

   "_id": ObjectId(1ad83e4k2ab8e0daa8ebde7), //mongo generated
   "parseId":ObjectId(1234567890),
   "title": "Mongo Migrate", 
   "description": "Migrating from Parse to Mongo"

Then if you need to match a document between the two databases later, you can write a script that goes along the lines of Parse.find({"_id": Mongo.parseId}).....

Upvotes: 1

Howard Lee
Howard Lee

Reputation: 987

MongoDB uses _id as primary key by default. _id has to be unique to avoid collision. The way you are generating unique ObjectId to _id is fine. As long as they are unique, you could even reduce the 20-character pad to save space.

Upvotes: 0

Related Questions