Capture MongoDB query output in a text file
As shown in the Helpful Commands and Queries article, querying MongoDB is indispensable for Swimlane Platform Troubleshooting.
At times the query output can be too large to manage within the command prompt console. The remedy is to use the script interpretation of Mongo Shell as follows:
Swimlane Platform Installer (SPI)
- Save the Swimlane namespace an env variable: NOTE: For SPI, enter default for <SWIMLANE_NAMESPACE>
- Save the mongo primary into an env variable:
- For Standalone:
- For HA:
- Use the command below to lookup the PRIMARY:
- Replace <MONGO_PRIMARY_POD> with name of the pod (ex: swimlane-sw-mongo-#) from the previous step:
- Place the diagnostic query in a text file with a .js filename extension.
- Assume that the script file is sample_query.js
- The scriptβs contents might look like this (and note the use of the cursor, while loop, print idiom recommended here):
- Copy the file sample_query.js into the PRIMARY mongo container:
- Run the file sample_query.js inside the mongo PRIMARY container. Invoke the Mongo Shell in this manner in order to cause the Shell to read in the script file, execute it, print the query results to the console, and route the console output to the destination text file:
- The output will be available in sample_query_results.txt and contain the query result text:
As an alternative, the script/query can, if small enough, be passed directly on the command line using the --eval parameter
Using mongo client:
Download all documents from the Settings collection
Output: Settings.json
Download a specific document from the Records collection filter by "Name"
Output: FD-1.json
Using mongoexport
Download Settings collection
Output: Settings.json
Download a specific document from the Records collection filter by "Name"
Output: FD-1.json
Depending upon the characters that appear in the script/query, some escaping may be necessary. Enjoy capturing query output in text files for easier diagnostics, and easier transmission to the Support team!