Q: Help! I’ve been using Studio 3T to regularly export MongoDB collections to track our international shipments, but when I went to check the data, I found it has been overwriting previous exports. While I’m rebuilding my historical data, I want to know is there any way to force scheduled exports to write to a uniquely named file each time?
Welcome to the latest Studio 3T Ask Manatees Anything – #Studio3T_AMA. Want to know how to use Studio 3T to make your MongoDB life better? Just tweet and use #Studio3T_AMA or email [email protected].
A: It is true that, historically, when an Export Task was run on Studio 3T, each Export Unit wrote its results to the file. That file is at the filename and path which is shown in the Export Unit’s destination field. If that file/filename wasn’t changed between runs of the Task, the files that were previously generated there are overwritten with the new results. The filename was unchanging by default.
Enter Placeholders For Export
Or at least that has been the case until Studio 3T 2021.6 and the introduction of placeholders. Placeholders are small text snippets that are automatically replaced with dynamic text when the export unit is used to write a file. There are three placeholders currently:
${datetime}
is replaced by the date and time in YYYY-MM-DD_HH-MM-SS format${date}
is replaced by the date in YYYY-MM-DD format${time}
substitutes the placeholder with the time in HH-MM-SS format
Where a filename field can make use of placeholders, you’ll find a ${}
icon next to it. Clicking on the drop box next to it will reveal all the placeholders available:
Selecting one of the placeholders in the menu here will insert the text into the file name at the current cursor position. You can type the placeholder string yourself if you wish. If you want to go back to the original generated filename, with no placeholders, just select Reset to default.
You can use a placeholder anywhere in the filename, including in the directory path. So say you have something like:
/home/dj/myexports/exports${date}/report1-${time}.json
This will create a date-stamped directory with the time-stamped exported file in it. Similarly you can make a date-time-stamped directory to hold all the named exported files in it.
Going Global
You won’t just find placeholders in the Export destinations fields. If you look in the Export Options, you’ll find you can use them as defaults in a task. This makes it easy to apply dynamic settings across entire Export Tasks.
In Studio 3T 2021.5, this setting for target was just one field, now it’s two, one for the directory and one for the filename, and both support placeholders. There’s also one extra placeholder for the filename field here. The ${exportsource}
placeholder is the name of the database and/or collection or query which is acting as the source of the data. You can mix that in with the data and time fields, or, as in the example above, just create a new dated directory for the export.
With placeholders you have the freedom to lay out your exports in a whole new organized structure on your disk. Or you can simply date-time-stamp your export files when you Export MongoDB data and never accidentally overwrite files again.