Hi,
I am using mongosh via mongoDbCompass (windows platform)… pasting multiple commands ignore all but the first, separating with a semicolon des the same, load(file.js) claims its not supported… what is the right way to run sevel consecutive commands in mongosh?
Hi @GUY_SOFFER_ZAMRANY1, it is possible to run multiple commands in the embedded MongoDB Shell in Compass. Semicolons and newlines are supported separators for commands. However, the shell output is only displayed for the last command run in the series of commands.
If you’d like to display the output for multiple commands, it’s possible to use print statements or console.log to display the output. For example, the following code displays 2 documents where 1 is displayed for each query:
var queensRestaurant = db.nyc.findOne({ "borough": "Brooklyn" });
console.log("Brooklyn restaurant:", JSON.stringify(queensRestaurant, null, 2));
var queensRestaurant = db.nyc.findOne({ "borough": "Queens" });
print("Queens restaurant:", JSON.stringify(queensRestaurant, null, 2));
We’re hoping to improve this experience and are open to any feedback. If you have any more questions, please don’t hesitate to reach out.