How to set working path, so I can write a script that pulls from database and writes locally to a csv or txt file

I wanted to see if I’m able to begin writing csv files locally using a query that I use in intellishell, but not sure how to change the path to where I can write locally after querying a live connection in studio 3t. I’ve also tried using “process.chdr()” , to no avail (output of intellishell output when trying to change directory).

Here’s an example of the code I’m trying to get to work to write locally, while connected live to a mongo db:

const startDate = ISODate(“2023-03-28”);
const endDate = ISODate(“2023-03-29”);
const cursor = db.getCollection(“yo_mama_db”).aggregate([
{ $match: { “createdDateTime”: { $gte: startDate, $lt: endDate }, “holdStatus”: {$exists:true} } },
{ $project:


createdDateTime: { $ifNull: [“$createdDateTime”,“”] },
ID: { $ifNull: [“$_id”,“”] },
xyz: { $ifNull: [“$”,“”] },



let output = “”;

cursor.forEach(function (row) {
if (output.length === 0) {
output += Object.keys(row).join(“,”) + “\n”;
output += Object.values(row).map(value => value instanceof Date ? value.toISOString() : (value === null ? “” : value)).join(“,”) + “\n”;

const filePath = “C:\Users\localpath”;
const file = new File(filePath);“w”);

Thanks for the help - Alex

Welcome to the 3T Community! :slight_smile:

I see that the “Use legacy shell” item is lit up in your screenshot, but process.chdr() is a Node.js function, so it’ll only work with mongosh (the “non-legacy” shell). Have you given that a try?

If I’m not completely wrong, the backslashes in this line need to be escaped, otherwise they become escape sequences. So the line should read const filePath = “C:\\Users\\localpath”;

Let me know if any of this is helpful. :slight_smile:


Thanks Rico!