How To
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> export ns=\<swimlane namespace> save the mongo primary into an env variable for standalone export mongo primary=swimlane sw mongo 0 for ha use the command below to lookup the primary kubectl n $ns exec swimlane sw mongo 0 mongo u admin p authenticationdatabase admin tls tlsallowinvalidcertificates admin eval="rs ismaster();" | grep primary replace \<mongo primary pod> with name of the pod (ex swimlane sw mongo #) from the previous step export mongo primary=\<mongo primary pod> 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) cursor = db logs find({level 'error'}) sort({date 1}) limit(3); while ( cursor hasnext() ) { printjson( cursor next() ); } copy the file sample query js into the primary mongo container kubectl n $ns cp sample query js $mongo primary /tmp/ 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 kubectl n $ns exec $mongo primary mongo quiet swimlane u admin p authenticationdatabase admin tls tlsallowinvalidcertificates /tmp/sample query js | tee sample query results txt the output will be available in sample query results txt and contain the query result text {"t" {"$date" "2022 01 25t12 42 44 533z"},"s" "w", "c" "network", "id" 23235, "ctx" "js","msg" "ssl peer certificate validation failed","attr" {"reason" "self signed certificate"}} { 	" id" objectid("61ef81c05e5c9c02d60d5207"), 	"date" isodate("2022 01 25t04 51 12 757z"), 	"hostname" "swimlane tasks 585997879f z7j4d", 	"layoutname" "fulldebug", 	"level" "error", 	"username" "swimlane system", 	"methodname" "core engines integration integrationjob performintegration", 	"linenumber" "138", 	"message" 	 	 } 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 kubectl exec swimlane sw mongo 0 mongo u admin p authenticationdatabase admin tls tlsallowinvalidcertificates swimlane eval="db settings find({}) pretty();" > settings json output settings json download a specific document from the records collection filter by "name" kubectl exec swimlane sw mongo 0 mongo u admin p authenticationdatabase admin ssl sslallowinvalidhostnames sslallowinvalidcertificates swimlane eval='db records find({"name" "fd 1"}) pretty()' > fd 1 json output fd 1 json using mongoexport download settings collection kubectl exec swimlane sw mongo 0 /bin/bash c "mongoexport u admin p authenticationdatabase admin ssl tlsinsecure db swimlane collection settings pretty" > settings json output settings json download a specific document from the records collection filter by "name" kubectl exec swimlane sw mongo 0 /bin/bash c "mongoexport u admin p authenticationdatabase admin ssl tlsinsecure db swimlane collection records pretty query='{\\"name\\" \\"fd 1\\" }' " > fd 1 json 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!