Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

Mongosh: How To Write More Files

Posted on: 16/02/2023 by Dj Walker-Morgan

Today, we’re looking at how to write files with Mongosh. This is new as previously writing files with the legacy Mongo shell was simply not possible.

As we mentioned in the first part of this series, with the new Mongosh and its Nodejs underpinnings now default in Studio 3T, that’s no longer a challenge. It opens the way to creating custom reports, web pages and more without leaving Studio 3T.

Why Should Mongosh Write Files?

In Studio 3T, you have at your fingertips, an extensive selection of export tooling, able to save collections in JSON, CSV, BSON dumps and more. But if you are faced with a system which can’t consume any of these formats, how can you export data to it without writing an app? Or you may want to create some output to go directly to the web. With no HTML export, you may think you have no option but to export in one format and write an application to generate the HTML.

Let’s look at a practical example. Say you’ve been asked to create a web page of companies and their websites from a collection – we’re going to use the MongoDB sample data and its sample_training/companies collection here. We only need the name and homepage_url where the url exists and isn’t null or empty.  We’ll create a query for that using Studio 3T’s Visual Query Builder:

The query that we want to use in Mongosh to write a file

We are projecting the fields we need and sorting by the name field. The next step is to turn that into a script. Click on Query Code and you’ll see a Mongo shell version of the query. 

Our query as Mongosh code, ready to go to the shell

Make sure the Language is set to mongo shell and then click on the shell prompt icon to open IntelliShell with this query.

Our query, now in IntelliShell ready for editing so we can make Mongosh write out a HTML file

We’re now in IntelliShell. The query runs and returns results, but we want to step through all the results ourselves. So, what we want to do is get what’s known as a cursor, a pointer into the results so that we can step through the data. Head up to the db.getCollection line and prefix it with var cursor= like so:

Converting the query to return a cursor we can use.

Now scroll to the end of the script where we can continue.

Getting Mongosh to Write Files

Before we can write to a file, we need to set which directory we want our file written to by setting the current working directory.

process.chdir("/Users/dj/");

Now we can open a file to write to. In the previous article on reading files, we used the fs package in Nodejs and we’re going to do that again here, but this time we’re going to get a file handle so we can keep writing to the file as we process each name and URL. For this we call up the fs.openSync() function.

var out = fs.openSync('page.html', 'w');

The first parameter to this is the name of the file we want to create, the second parameter says what we want to do with this file handle. In this case, it’s w for write.

The next step is writing out some HTML prologue to the file. We’re doing the bare-minimum here for the page to appear and we are using the fs.writeSync() function to do it.

fs.writeSync(out,"<html><body><table>\n");

The first parameter of writeSync is the handle of the file we want to write to. The second parameter is what we want to write. There’s no automatic line endings added, so we include “\n” at the end to add a new line in. 

 And now we are ready to start processing our results.

Stepping through the Results

The cursor we got earlier has a function which tells you if it has any more data to return – hasNext() – and a Next() function which gives you that data. That means that in JavaScript/Mongosh all you need is a simple while loop that tests if there’s any more data:

while (cursor.hasNext()) {

Now we know there’s a document to be read from the results we can use Next() to get it:

   var r = cursor.next();

And now we can write that document out to our file using fs.writeSync() again.

    fs.writeSync(out,`<tr><td><a href='${r.homepage_url}'>${r.name}</a></td></tr>\n`);

There’s a bit to unpack here with the string being passed to writeSync. It’s what’s called a JavaScript Template Literal string. These are denoted by the back quotes surrounding them. The magic with template literal strings is that you can directly embed variable names in them, surrounded by ${ and }. The values of those variables will appear in the string. It saves a lot of fiddly string appending and formatting. So, in our example, we are printing out a HTML table row “<tr>” and then table item “<td>” and then an anchor “<a href='“. We then embed ${r.homepage_url} which will be the URL for the company and close up the anchor with a “'>“. The text for the link is embedded as ${r.name} and finally we close the anchor, the table data and the row “</a></td></tr>\n“. Write that and it makes a row in a table with a linked name in it.

Now we just need to close the while loop we started earlier.

}

And now, we finish and tidy up. We close the cursor to release any resources:

cursor.close();

Write the last of the HTML closing the table, the body and the html tag to our output file and wrap it all up closing the output file handle.

fs.writeSync(out,"</table></body></html>\n")
fs.closeSync(out);

Run this script and you’ll have a page.html file ready to open in your browser or copy to a website. 

Other Ways for Mongosh to Write Files

There are other ways to write to a file. For example, fs.writeFileSync() will write a string straight to a file in one operation. The problem with that is the more data you have, the more memory you use. By using a cursor and writing each line to file, we get to be able to process much bigger files, bigger than your system’s memory. The important thing is that you have the option to pick which one is right for your task.

So that’s writing files, a completely customisable way of getting data out of your database, using Studio 3T’s Visual Query Builder (and this works with Aggregation Editor too) and IntelliShell. Enjoy your new freedom. In the next part, we’ll look at networking with Mongosh inside Studio 3T.

Code Complete

Here’s all the code from the example if you want an easier to read reference:

db = db.getSiblingDB("sample_training");
var cursor=db.getCollection("companies").find(
    {
        "$and" : [
            {
                "homepage_url" : {
                    "$exists" : true
                }
            },
            {
                "homepage_url" : {
                    "$ne" : null
                }
            },
            {
                "homepage_url" : {
                    "$ne" : ""
                }
            }
        ]
    }, 
    {
        "name" : 1.0,
        "homepage_url" : 1.0
    }
).sort(
    {
        "name" : 1.0
    }
);

process.chdir("/Users/dj/");
var out = fs.openSync('page.html', 'w')
fs.writeSync(out,"<html><body><table>\n")
while (cursor.hasNext()) {
    var r = cursor.next();
    fs.writeSync(out,`<tr><td><a href='${r.homepage_url}'>${r.name}</a></td></tr>\n`);
}
cursor.close();
fs.writeSync(out,"</table></body></html>\n")
fs.closeSync(out);

How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Dj Walker-Morgan

Dj has been around since Commodore had Pets and Apples grew everywhere. With a background in Unix and development, he's been around the technology business writing code or writing content ever since.

Related articles

  • Mongosh: How To Read More Files 
  • Mongosh: How to Run More Processes
  • How do you make Studio 3T export MongoDB collections to unique files? #Studio3T_AMA
  • Configure mongodump, mongorestore & mongo shell Executable Files
  • Studio 3T now defaults to the Mongosh shell

Tags

2022 academy aggregation AMA atlas Certification christmas community connections culture date tags events export features hackolade import intellishell In Use JSON knowledge base migration modelling mongodb mongoodb mongosh My 3T productivity query regex releases schema security SQL Studio 3T tasks time series tips updates webinar windows

Browse by MongoDB topic

  • Connecting to MongoDB
  • Database Administration & Security
  • Getting Started with MongoDB
  • Getting Started with Studio 3T
  • Import/Export
  • Job Automation & Scheduling
  • MongoDB Aggregation Framework
  • MongoDB/Studio 3T Workshops
  • Performance
  • Query & CRUD Operations
  • Reference
  • Schema
  • Studio 3T Licensing
  • Support and other resources
  • Working with MongoDB & SQL
  • Working with MongoDB Atlas

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2023 3T Software Labs Ltd. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum

We value your privacy

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data.

By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
Accept all
Decline all
Manage cookies
✕

Privacy Preference Center

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in our Privacy Policy.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Google Analytics

Google Ads

Bing Ads

Facebook

LinkedIn

Quora

Hotjar

Reddit

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

HubSpot

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected