How to Batch Insert JSON Data into MongoDB using Mongoose

I’m currently working on a project that uses a large amount of JSON data (roughly 1200 objects) to populate records in a MongoDB collection. The data contains most of the information I need, but it was not formatted according to the model that I’ve created for the project.
I spent a little bit writing a program that would convert my original JSON data into a new array of objects that are ready to be loaded into MongoDB records. Once the data was properly formatted, I used Mongoose’s handy Model.insertMany() to take my array of JSON objects and pass them into the collection.
The key part of my code looked like this:
const meetings = fs.readFileSync(__dirname + '/northAmericaMeetings.json', 'utf-8');async function loadMeetings() {
try {
await Meeting.insertMany(meetings);
console.log('Done!');
process.exit();
} catch(e) {
console.log(e);
process.exit();
}
};
Everything should have been working great, but when I ran my program, I got this error:
if (obj && ‘_id’ in obj) {
^TypeError: Cannot use ‘in’ operator to search for ‘_id’ in
What the heck did this mean? I spent hours googling and reviewing documentation, but I couldn’t discern the meaning of this error.
Eventually, I was able to determine that my problem lay in the way I was forming my meetings array. As defined above, meetings represented JSON text. But it turns out that Mongoose’s insertMany() couldn’t read that, since it wasn’t actually JSON, but rather a string.
In order to convert my JSON string into a bone fide JSON object, I needed to use JSON.parse(), like so:
const meetings = JSON.parse(fs.readFileSync(__dirname + '/northAmericaMeetings.json', 'utf-8'));Once I parsed my JSON text into a JSON object, Mongoose was able to handle it, and the previous error disappeared. Mysterious, a little silly, and very important to remember for the future.
So the next time I see an error like this, I’ll definitely make sure that my JSON has been properly parsed!
Does the way JSON needs to be handled, with JSON.parse() and JSON.stringify(), ever confuse you? What are the errors you watch for that alert you this may be a problem?
