How to mongoimport and not use ObjectId in the _id fields

I would like to know how to run mongoimport to import a JSON file into Meteor app’s MongoDB, and not use ObjectId in the _id fields.

I have a JSON array file seed.json that I sometimes import to my database. I run:

mongoimport --db myApp --collection myCollection --drop --file seed.json --jsonArray

The data is imported without problem. But the documents use ObjectId in _id fields rather than just a string. This means I can’t find the documents by _id because Meteor expects _id to be string. So I made a makeshift method that does the following:

...
let data = JSON.parse(Assets.getText('seed.json'));
data.forEach(elm => {
  MyCollection.insert(elm);
});
...

But is there a way that I can use mongoimport or something equivalent instead?

hey I have same problem. Could you figure it out ? :smiley:

I concluded that it is not possible to do what I wanted to do using mongoimport. Ended up using the second method.

See how I did it in this file: https://github.com/remotebase/remotebase-mantra/blob/master/server/methods/debug.js#L29-L43

Did you try to assign an _id field in the json. From my understanding, mongo would just use the specified _id.

mongoimport --db myApp --collection temporaryCollection --drop --file seed.json --jsonArray

From mongo shell

db.temporaryCollection.find().forEach(function (document) {
    document._id = document._id + ''
    db.myCollection.insert(document)
})
1 Like