InsertMany in MongoDB via file

To do that I created a js file. And wrote some MongoDB command to select the database. Then drop the collection if it exists or create a new collection. To do that I opened the js file and wrote

db = db.getSiblingDB("myDb");
db.newColl.drop();
db.NewColl.insertMany([
....... records ........
....... records ........
....... records ........
....... records ........
....... records ........
]);

Once the file is saved in the current folder. I reopened the MongoDB from the current folder location. And after I entered the MongoDB shell, I run this command.

load(theFile.js)

This command added all the records from the file in the collection newColl in myDb

Leave a comment

Your email address will not be published.