Categories
MongoDB

InsertMany in MongoDB via file

while studying for mongoDb certification I got a dataset file from the instructor. I had to fill my database with the data provided in that file. I already had the database created and the data which was there in the file was to be contained in a collection.

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 Reply