How to debug shell scripts

Hello all,
I am writing a js scripts in my_test_scripts.js but when I run the scripts by:
mongo localhost:27017/test my_test_scripts.js,
I get some log messge below:
MongoDB shell version v4.2.0
connecting to: mongodb://localhost:27017/tests?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“8796fc44-221d-4862-8d6f-e439fd77b227”) }
MongoDB server version: 4.2.12
I don’t know what’s happening in this case, is there any way to debug the scripts?

Many thanks,


You can try adding some debug statements in your JavaScript file. I have tried this and it works fine. For example, if the script is:

var doc = { a: "str-2" }
doc = db.test.insertOne(doc)

When you run it, you wont see any output indicating if the insert is success (or failure). By adding the following you can see what is the result of running the script:

var doc = { a: "str-2" }
doc = db.test.insertOne(doc)

Similarly, with the statement db.test.findOne() can be coded as printjson(db.test.findOne()) so that you can see the output of the query.

NOTE: The new (Beta) MongoDB Shell mongosh has the Retrieve Shell Logs feature. The logs for each session are stored and you can view them.