Introducing APIs

If you’ve heard the term API before, chances are it’s been used not to refer to APIs in general, but instead to a specific kind of API, the web API. A web API allows for information or functionality to be manipulated by other programs via the internet. For example, with Twitter’s web API, you can write a program in a language like Python or Javascript that can perform tasks such as favoriting tweets or collecting tweet metadata. In programming more generally, the term API, short for Application Programming Interface, refers to a part of a computer program designed to be used or manipulated by another program, as opposed to an interface designed to be used or manipulated by a human. Computer programs frequently need to communicate amongst themselves or with the underlying operating system, and APIs are one way they do it. In this tutorial, however, we’ll be using the term API to refer specifically to web APIs.

In general, consider an API if:

  • 1. Your data set is large, making download via FTP unwieldy or resource-intensive.
  • 2. Your users will need to access your data in real time, such as for display on another website or as part of an application.
  • 3. Your data changes or is updated frequently.
  • 4. Your users only need access to a part of the data at any one time.
  • 5. Your users will need to perform actions other than retrieve data, such as contributing, updating, or deleting data.

If you have data you wish to share with the world, an API is one way you can get it into the hands of others. However, APIs are not always the best way of sharing data with users. If the size of the data you are providing is relatively small, you can instead provide a “data dump” in the form of a downloadable JSON, XML, CSV, or SQLite file. Depending on your resources, this approach can be viable up to a download size of a few gigabytes. Remember that you can provide both a data dump and an API, and individual users may find one or the other to better match their use case. Open Library, for example, provides both a data dump and an API, each of which serves different use cases for different users.

As we’ve learned, documentation is a user’s starting place when working with a new API, and well-designed URLs make it easier for users to intuitively find resources. Because they help users to quickly access information through your API, these elements—documentation and well-conceived URLs—are the sine qua non of a good API. We’ll discuss these elements in greater depth later in this tutorial. As you use other APIs in your research, you’ll develop a sense of what makes a good API from the perspective of a potential user. Just as strong readers often make strong writers, using APIs created by others and critically evaluating their implementation and documentation will help you better design your own APIs.

Advantages of APIs:

  • Flexible document schemas.
  • Code-native data access.
  • Change-friendly design.
  • Powerful querying and analytics.
  • Easy horizontal scale-out.

Disadvantages of APIs:

  • APIs can use up high amount of memory.
  • There is a limit for document size, i.e. 16mb.
  • There is no transaction support in APIs.

This section contains a guide for how to link into an API

Install API

Download the binaries from the MongoDB Download Center

1. Open Windows Explorer/File Explorer.

2. Change the directory path to where you downloaded the MongoDB .msi file. By default, this is %HOMEPATH%\Downloads.

3. Double-click the .msi file.

4. The Windows Installer guides you through the installation process. If you choose the Custom installation option, you may specify an installation directory. MongoDB does not have any other system dependencies. You can install and run MongoDB from any folder you choose.


This tutorial assumes that you installed MongoDB in C:\Program Files\MongoDB\Server\4.2\.

Run MongoDB

First Step

Set up the MongoDB environment.

MongoDB requires a data directory to store all data. MongoDB’s default data directory path is the absolute path \data\db on the drive from which you start MongoDB. Create this folder by running the following command in a Command Prompt:

                                md \data\db

You can specify an alternate path for data files using the --dbpath option to mongod.exe , for example:

                                        "C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath d:\test\mongodb\data

If your path includes spaces, enclose the entire path in double quotes, for example:

                            "C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" --dbpath "d:\test\mongo db data"

You may also specify the dbpath in a configuration file.

Second Step

Start MongoDB.

To start MongoDB, run mongod.exe. For example, from the Command Prompt:

                            "C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe"

This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.

Depending on the security level of your system, Windows may pop up a Security Alert dialog box about blocking “some features” of C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe from communicating on networks. All users should select Private Networks, such as my home or work network and click Allow access. For additional information on security and MongoDB, please see the Security Documentation.

Third Step

Verify that MongoDB has started successfully

Verify that MongoDB has started successfully by checking the process output for the following line:

                            [initandlisten] waiting for connections on port 27017

The output should be visible in the terminal or shell window.

You may see non-critical warnings in the process output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB.

Fourth Step

Connect to MongoDB.

To connect to MongoDB through the ~bin.mongo.exe shell, open another Command Prompt.

                            "C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"


PyMongo is a Python module used to interface between MongoDB and Python applications. To install the PyMongo library, install onto your version of Python from command line using the following command:

                        pip install pymongo

To import the PyMongo library into your scripts, use the following code to have access to it as a reference:


You'll generally want to use the MongoClient object to gain access to the mongod instance while it is running.


You can access a particular database using the MongoClient object. From there, you can also search for particular collections(tables) within the database as well.


MongoDB uses documents, written in BSON, which act as database records. You can insert either one document or multiple at a time. These can also be queried, updated, deleted, much like any other CRUD operations in a typical database.


Source: Tutorial - PyMongo