Converting ObjectIDs into string _id's

I pulled in all my data into compose through an import function and it generated objectIDs for each record. I realize I lose out on Meteor benefits with them and if I were to continue down the path with them I would have to ensure my new records where generating ObjectIDs.

My question is, is there any way to convert these ObjectID’s to the ID type that Meteor uses?

I have this same / similar issue and question.

I copied a set of data in my db, and when it copied, it created the new _id as an ObjectId instead of the string id I want / need / check for when I call my methods.

Is there a mongo script, or way to convert those ObjectIds back into meteor string Ids?

Example:
I have this

"_id" : ObjectId("5c4346abc6c7a39e087e2f2a")

but want / need this

"_id" : "5c4346abc6c7a39e087e2f2a"

I saw someone posted that in spacebars you can do

{{helper._id._str}}

to display the string id if needed, but I actually want to change the _id in the collection.

Any help is appreciated.

Just a thought here, I didn’t run in this situation.

But I think you’ll need to write a function that loop through the docs and for each doc you’ll need to clone it and insert a new id using Meteor.random() then delete the old one.

@alawi Ok, I was thinking of just writing some javascript to do this going forward, but wasn’t sure if there might be a way to change the ones there. I do see that _id is an immutable field, so figured there wasn’t a way to just convert it.

I appreciate it.

1 Like

This is what we use in a webhook that uses MongoClient: https://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html#toHexString

2 Likes

Nice @alawi. Thank you sir.

I just had to perform this operation, the easiest way for a small amount of ID’s is to use Studio 3T. Copy the document into notepad, change the ID, JSON Import from clipboard, delete original – I did 10 of these in 5 minutes.