Search code examples
node.jsmongodbmeteorobjectid

generate Meteor.JS ObjectID outside of Meteor


I'm trying to import a large quantity of data through a NodeJS script into my Meteor App Database using the NodeJS mongoDB driver.

I need to generate a Meteor ObjectID in the script. I know that I can configure my Meteor App to generate Ids as Mongo does, but I would like to avoid to do so because it would imply a lot of changes in my app.


Solution

  • You can just use mongodb driver in your script to generate ObjectIds as described in the docs here.

    Example:

    var ObjectID = require('mongodb').ObjectID;
    
    _id: new ObjectID()
    

    Because Meteor use hexadecimal Strings for the IDS you need to use :

    var ObjectID = require('mongodb').ObjectID;
    
    _id: new ObjectID().toHexString()