How to execute external file using mongo shell and see the result in console?
I have external file, like query.js
and I would like to execute it and see the result in cmd.
Let's say, content of the file is:
db.users.find()
If you want to check your databases list, use the command show dbs. Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it. In MongoDB default database is test.
Select All Fields Just type a snippet prefix "run", and press "tab" to insert this snippet. Just execute the query by clicking the execute button or use the "Command-Enter" keyboard shortcut. This would produce the result as shown below. NoSQLBooster for MongoDB provides in-place editing in result tree view.
mongorc. js File. When starting, mongo checks the user's HOME directory for a JavaScript file named .
Put this into your query.js
file:
function get_results (result) { print(tojson(result)); } db.col.find().forEach(get_results)
and run:
mongo db_name query.js
Here's a good explanation why you should do it this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With