Bulk Insert -- WHY EVEN BOTHER if the _id's are set to Mongo Objects

What a waste of a day! Built this beautiful csv import system, only to find out the the _id: object(string)

This sucks.

Best I got is having to rewrite a bunch of front end code to support these ID types… here ya go kids:



function convertToObjectId (id) {
    // Check if id is in the format ObjectID("..."), extract the actual ID if needed
    if (typeof id === 'string' && id.startsWith('ObjectID(')) {
        id = id.match(/ObjectID\("(.*)"\)/)[1]; // Extract the ID part from the string
    }

    // Try to convert to Mongo ObjectID
    try {
        id = new Mongo.ObjectID(id);
    } catch (e) {
        console.error('Invalid ObjectID format:', e);
    }

    return id;
}

// Helpers
Template.contactModal.helpers({

	'findContact'() {
        window.tagsViewMode = 'full';

        // Directly call the global helper convertToObjectId
        const contactId = convertToObjectId(window.contactModalId);

        // Perform the query
        return Contacts.find({ _id: contactId });
    },

Its the price to pay if you want massive bulk imports… In my case, 5 million.

That a common issue we had too. After the first time it happened we just added ids to each doc on insert.

But that should be handled automatically be meteor.

Yeah I tried that but things got even weirder, because there’s the possibility to upsert.

Whatever… only took 6 hours to fix.

There is an option or a package to use the ObjectID instead of the default string id for MongoDB.

Which function in Meteor is not doing this?

bulkWrite. Is not expected to do it as is not supported by Meteor, but it will be nice for it to be.

Yes bulkWrite; NECESSARY for scale application…

I was only having issues on some front end code that I wrote that isn’t actually ‘native meteor’; I was storing ID’s as a localStorage option so users could remember stuff in their UI between screens.

I think actually it is quite well supported, just my code was using it wrong in the end.