[MongoDB] How to execute queries from Terminal with arguments
I’ll introduce how to execute MongoDB queries from the Terminal or command line by passing arguments.
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 = '[email protected]';" 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.