[MongoDB] How to execute queries from Terminal with arguments
Tadashi Shigeoka · Thu, April 11, 2019
I’ll introduce how to execute MongoDB queries from the Terminal or command line by passing arguments.
Mongo Query Terminal Execution Method
Here’s sample code for executing MongoDB queries from the Terminal or command line with arguments:
find_user_id_by_email.js
// Query to get specific user id from email address
//
// Usage:
// mongo test --quiet --eval "var _email = 'youremail@example.com';" find_user_id_by_email.js
(function(){
var user = db.users.findOne({ email : _email });
if (user && user._id) {
print(user._id.str);
} else {
print(new Error('Not Found'));
}
})();
That’s all from the Gemba, where we want to execute MongoDB query files from Terminal with specified arguments.
