---
title: Capture MongoDB query output in a text file
slug: user-guide-10x/capture-mongodb-query-output-in-a-text-file
docTags: 
createdAt: 2025-12-12T21:37:29.125Z
---

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)

1. Save the Swimlane namespace an env variable:
   NOTE: For SPI, enter default for \<SWIMLANE\_NAMESPACE>

:::BlockQuote{indent="1"}
export NS=\<SWIMLANE\_NAMESPACE>
:::

2. Save the mongo primary into an env variable:

   1. For Standalone:

   :::BlockQuote
   export MONGO\_PRIMARY=swimlane-sw-mongo-0
   :::

   2. For HA:

      1. Use the command below to lookup the PRIMARY:

      :::BlockQuote
      kubectl -n $NS exec swimlane-sw-mongo-0 -- mongo -u Admin -p --authenticationDatabase admin --tls --tlsAllowInvalidCertificates admin --eval="rs.isMaster();" | grep primary
      :::

      2. Replace \<MONGO\_PRIMARY\_POD> with name of the pod (ex: swimlane-sw-mongo-#) from the previous step:

      :::BlockQuote
      export MONGO\_PRIMARY=\<MONGO\_PRIMARY\_POD>
      :::
3. Place the diagnostic query in a text file with a .js filename extension.

   1. Assume that the script file is sample\_query.js
   2. The script’s contents might look like this (and note the use of the cursor, while loop, print idiom recommended here):

   :::BlockQuote
   cursor = db.Logs.find(\{Level:'Error'}).sort(\{Date:-1}).limit(3);
   while ( cursor.hasNext() ) \{
   &#x20;  printjson( cursor.next() );
   }
   :::
4. Copy the file sample\_query.js into the PRIMARY mongo container:

:::BlockQuote{indent="1"}
kubectl -n $NS cp sample\_query.js $MONGO\_PRIMARY:/tmp/
:::

5. 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:

:::BlockQuote{indent="1"}
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
:::

6. The output will be available in sample\_query\_results.txt and contain the query result text:

:::BlockQuote{indent="1"}
\{"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"}}
\{
&#x9;"\_id" : ObjectId("61ef81c05e5c9c02d60d5207"),
&#x9;"Date" : ISODate("2022-01-25T04:51:12.757Z"),
&#x9;"HostName" : "swimlane-tasks-585997879f-z7j4d",
&#x9;"LayoutName" : "FullDebug",
&#x9;"Level" : "Error",
&#x9;"UserName" : "swimlane\_system",
&#x9;"MethodName" : "Core.Engines.Integration.IntegrationJob.PerformIntegration",
&#x9;"LineNumber" : "138",
&#x9;"Message" : ...
&#x9;....
&#x9;...
}
:::

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

:::BlockQuote
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"

:::BlockQuote
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

:::BlockQuote
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"

:::BlockQuote
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!
