how to load .js file in mongodb

673 views
Skip to first unread message

Faraz Usmani

unread,
Jun 20, 2017, 7:31:48 AM6/20/17
to mongodb-user
hello all

i am new in mongodb i have a file homework2.js which i want to load in mongodb, i have tried both method

load("homework2.js")
load("c:\mongodb\script\homework2.js")
i have installed mongodb in root c drive in mongodb folder and database path c:\mongodb\data\db

i want to confirm which folder i will put this file and what command to load the said file.


Kevin Adistambha

unread,
Jun 23, 2017, 2:12:00 AM6/23/17
to mongodb-user

Hi Faraz

You can use pwd() to check your current working directory within the mongo shell. You can also use ls() to list the contents of the current directory. To load a file into the mongo shell, you can use the load(<filename>) method.

You may find these links helpful:

Best regards,
Kevin

Bhaskar Sarma

unread,
Jun 25, 2017, 4:13:14 AM6/25/17
to mongodb-user
You can use mongoimport utility,

mongoimport --db <<dbname>> --collection <<collection_name>> <homework2.js

Bhaskar.

Kevin Adistambha

unread,
Jun 28, 2017, 1:24:48 AM6/28/17
to mongodb-user

Hi

Just to clarify, you can use the mongoimport tool if the homework2.js file contains data to be imported into MongoDB (e.g. JSON documents, CSV-formatted data, etc.). However, if the homework2.js file contains Javascript functions, the functions can be loaded into the mongo shell using the load() method. If you need to run a Javascript file, you can specify the file as a parameter to the mongo shell, e.g. mongo script.js (see https://docs.mongodb.com/manual/reference/program/mongo/#bin.mongo)

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages