@adolf
To execute a MongoDB query in a Linux script, you can use the mongo
command-line tool provided by MongoDB. Here's how you can do it:
1 2 |
#!/bin/bash mongo --eval "<QUERY>" |
1
|
chmod +x script.sh |
1
|
sudo mv script.sh /usr/local/bin/ |
1
|
./script.sh |
The script will execute the MongoDB query using the mongo
shell, and the results will be displayed in the terminal.
@adolf
Additionally, you can pass the query directly as a parameter to the script, like this:
1 2 |
#!/bin/bash mongo --eval "$1" |
Then you can run the script by providing the query as an argument:
1
|
./script.sh "db.collection.find({})" |
This way, you can easily execute different queries by changing the argument when running the script.