Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo shell execute query from file and show result

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() 
like image 719
Andrei Avatar asked May 01 '13 20:05

Andrei


People also ask

How do I show DBS in MongoDB?

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.

How do I run a selected query in MongoDB?

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.

Which file run the MongoDB shell?

mongorc. js File. When starting, mongo checks the user's HOME directory for a JavaScript file named .


1 Answers

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.

like image 164
alecxe Avatar answered Sep 24 '22 21:09

alecxe