Working with ObjectId in reference documents

First of all, I would like to thank this awesome community that has helped me immensely wrt each query I have posted here. I have a query that pertains to ObjectId in MongoDB, especially when I am migrating database from Firebase to MongoDB. I am using aggregation pipeline in my queries, and due to the nature of my queries I am facing a dilemma wrt _id of each document and their references in other collections.

Consider a user document

  _id: ObjectId('2456177899'),
  name: ABC.
  age: 28,

and now consider an “Employment” document that has reference to user _id

 _id: anthing
 userId: '2456177899'

and as expected, it dosen’t work in $lookup, I applied the following method, in order for it to work

"let": { "userId": { "$toObjectId": "$userId" } },

and it works, but I am migrating data from firebase database, and during migration process, I am inserting firebase unique key as _id instead of ObjectId for example

_id: '-Msdae23124asd'

First option is to ensure that any new entry in migrated database conforms to ObjectId, for that to happen I will have to put checks in every controller, and MongoDB queries like I am doing with aggregation pipeline.

Second option is to migrate firebase data with key as ObjectId, and save each reference id as ObjectId, for example “Employement” document will look like this

 _id: anthing
 userId:  ObjectId('2456177899')

This will save a lot of checks and conditions (which in some cases might be missed wrt future data entry), what is the best approach for my scenario, save each reference to _id as ObjectId in other collections or add checks etc in order for everything to work.

Thanks in advance

Hey, unfortunately I have no answer for you but a question.
I’m facing more or less the same problem as you and wanted to ask you how you referenced the objectId from user to userId of employment?