Tag archive for: MongoDB

How to paginate MongoEngine records in your Python 3 Flask application

Flask and MongoEngine help makes development work easier.

One common task in the development of backend applications is the pagination of database records. Without pagination, the application server can run out of memory while generating a response from the database records.

This post discusses a way to paginate MongoEngine records in your Python 3 Flask application using the facilities provided by the Flask-MongoEngine extension.

How to migrate your MongoDB database instance with mongodump, mongorestore, tar and scp

Migration of MongoDB database is part and parcel of DevOps, especially when you are running your own projects.

MongoDB provides us with two utilities for performing database migration – mongodump and mongorestore.

A simple MongoDB database migration can be performed in 5 steps:

  1. Use the mongodump command to export the data of a MongoDB database instance as files in the source server’s filesystem.
  2. Use the tar command to compress the exported files as a single .tar.gz file in the source server’s filesystem.
  3. Use the scp command to send the .tar.gz file from the source server to the destination server.
  4. Use the tar command to decompress the .tar.gz file at the destination server.
  5. Use the mongorestore to import the extracted files into the destination MongoDB database.

This post discusses how you can perform MongoDB database migration with utilities provided by MongoDB and most Linux servers.

How to enable authenticated MongoDB access for Flask-MongoEngine applications

After having a first look at MongoDB more than 5 years back, I told myself that I will use MongoDB to realise the next functionality of Techcoil.

With exposure to Python 3, Flask and MongoEngine in 2017, I had acquired the skills to build a microsite to recommend gift ideas. This microsite uses MongoDB to store the gift recommendation data.

With Flask and MongoEngine, development of this microsite did not take too much leisure time. With MongoDB not enforcing authentication, there were not much hindrance in setting up the development environment for this microsite.

However, this free-for-all mode of accessing MongoDB is not recommended for production environments. Without authentication, it is easier for ill-intentioned people to mess up the backend database.

To ensure that I have a go-to post for implementing authenticated access to MongoDB backed projects in the future, I document the steps needed for Flask-MongoEngine applications to access MongoDB instances with access control turned on.

Getting documents from MongoDB collections with PHP

We have a MongoDB server running as a windows service on the same machine as our web server. Over time, this MongoDB server had been listening on port 33333 to help us remember information that we had collected about our friends.

As time passes, our memory can hardly rival that of our MongoDB server, which was designed to help us recollect information efficiently. This post documents some proof of concept that I did for querying documents from MongoDB collection via the PHP driver.

Inserting a document into a collection in MongoDB with PHP

Assume that we have a MongoDB server installed as a windows service on the same machine as our web server. The server listens on port 33333.

We met a new friend, and we want to save some details about her. That night, before bidding farewell, she said: “Remember me, I am Mary Jane. You can write to me at mary.jane@gmail.com“.

Although that’s all we have about her, this is sufficient for us to insert a document about Mary into our MongoDB database.

Inserting documents into collections in MongoDB with PHP consists of the following steps:

  • Derive an instance of the MongoCollection class that represents the MongoDB collection to insert documents.
  • Define the document to insert.
  • Define additional options for the insert.
  • Execute the insert function of the MongoCollection instance.

Connecting to and disconnecting from a MongoDB server in PHP

The first step to manipulate data in the MongoDB ecosystem is to connect to a MongoDB server. In php, we can use the Mongo class to help us connect to one or more MongoDB servers.

For the purpose of this demonstration, let’s assume that we had installed a MongoDB server instance as a windows service which listens on port 12345. In addition, our PHP web server runs on the same machine as the MongoDB instance.

Installing MongoDB as a windows service

After using MongoDB for quite a while, I realised that I had been repeating the starting up of the MongoDB database daemon whenever I reboot my PC. Manually starting the MongoDB database daemon via my command prompt whenever I want to work on my projects is inefficient.

Hence making MongoDB run as a windows service automatically when my computer starts up is one way to avoid repeating myself. In this post, I document how I install my MongoDB database as a windows service.