MongoDB and Firestore: differences and scenarios

Written by

As options proliferate, choosing between different databases can be complicated. This article will help you choose between two popular ones: MongoDB and Firestore. We’ll go over each of their features and what makes one better than the other for different use cases.

How are they similar?

First, both engines are NoSQL databases because of the document-object data structure they use to store data. This means that the way that the data is stored is the same as the way JavaScript handles objects. For example, MySQL uses tables as an entity, while MongoDB and Firestore use objects called documents as entities. One thing to keep in mind is that MongoDB accepts query language to retrieve data, but Firestore has its own method and API calls to do so.

MongoDB FireStore
Fully Managed/Automated Database Service Fully Managed/Automated Database Service
Global Clusters Global Clusters
Monitoring and Alerts Monitoring and Alerts
Serverless Triggers Serverless Triggers

  • Fully Managed/Automated Database Service: Both MongoDB and Firestore are managed services. They offer the developer the liberty to spend more time working with data and less time configuring the server and infrastructure.
  • Global Clusters: Google data centers are distributed on every continent. MongoDB also has its own global clusters around the world.
  • Monitoring and Alerts: MongoDB has its own metrics and alerting service. Firestore utilizes Stackdriver to accomplish the same functionality.
  • Serverless Triggers: Event-driven triggers in MongoDB can be related to cloud Pub/Sub in Firestore. This means that in MongoDB, you can create triggers that start working when some rules are fulfilled in the database and respond in real-time with server-side logic, while cloud Pub/Sub can be configured to listen to specific events in the whole cloud to execute Cloud Functions logic similar to the event-driven triggers in MongoDB.

Differences and Features

In order to understand the differences between these two technologies, we have to emphasize the most important features that make each unique.

Automated Backups

The Mongo service offers automated daily backups to the databases managed by the Atlas service. It also offers incremental data recovery and consistent, cluster-wide snapshots of sharded clusters. Firestore’s similar service, “Scheduled export”, has to be configured and implemented manually by the administrator. In the other hand, Firestore can be configured to export all its data to a cloud storage bucket, almost like a backup but harder to recover in a case of a failure or the need to roll back the data because you have to manually update all your database, in this case, MongoDB feature is better for backing up.

Security

Mongo has a unique level of security for its Atlas service. Firestore has its own security rules that work slightly differently:

  • Rules by document: Firestore can assign user-specific data and protect it from other users from reading. This functionality can be accomplished by Mongo, creating as many database users as final users, which can be a struggle in many cases.
  • Connection: Mongo uses its own port, which can be configured to use SSL/TLS. Firestore uses https request to its API.
  • Private Link: While Firestores works over the Google Cloud Platform and all its services are connected internally inside the Google Cloud, Mongo uses Private Link, which provides exclusive access to AWS.

Realtime updates

This is one of the most important features that distinguishes Firestore from other services. The capability to update and listen to changes in the database in real-time is pretty neat when working with web/mobile applications, giving all the users the feeling they are updated all the time. Mongo can be integrated with other technologies to make this possible, but it requires a lot of work and a very specific infrastructure. For example, an application that keeps a user’s to-do list and syncs between the mobile and web versions of the app would best utilize Firebase because of its ability to provide real-time updates.

Offline Support

Working with Firestore gives the developer the ability to enable offline support to save data locally and then sync it with the cloud whenever the connection is back. Offline data is not new to web and mobile applications; the difference here is that this feature already comes out of the box using the Firestore library. Let's imagine that you have to create a web application for local businesses to manage their inventory, sales, and providers. This sounds like a more robust application, so for this multi-purpose approach you should be using MongoDB, since it can be installed in your own server and doesn't need to have internet access to store or process data. MongoDB can also be installed in Linux, Windows, Solaris, and OS X, which means a server application can be developed locally without needing any remote connections.

The ideal tool for every job

Here are some reasons you might want to pick each Mongo over Firestore, or vice versa:

1. You’re a small team or an independent dev

While MongoDB has the Atlas service to offer a cloud solution for its database, Firestore has an entire development platform included in Firebase. This simplifies many things and gives more time to the developers to focus on coding. For example, Firebase’s hosting service provides a complete hosting solution where you can deploy your application assets directly from your code. It already has https and a domain where other applications can interact and consume its APIs. On the back end, Firebase provides Cloud Functions — simple code snippets executed on the cloud so you don’t have to worry about how to get a server to handle requests. 

2. You need your app to be blazing fast

MongoDB has the ability to respond to requests faster than most other NoSQL databases, and it usually outperforms Firestore in processing read/write/update operations. That’s due to its proprietary protocol which converts the data and transfers it on its own port, while Firebase utilizes the https protocol which can be slower for the encryption associated with it. Also, the MongoDB engine was designed to work with light to heavy data transactions, but Firestore was designed to handle mobile applications data, which is often much lighter than working with blobs and analytics in real-time — the type of data MongoDB was designed to work with.

3. You’re looking to scale across regions

Firestore can scale automatically in multiple regions with data replication and strong consistency. MongoDB Atlas, on the other hand, offers cross-region replication, distributing nodes around the world. The key difference here is that MongoDB has a fault-tolerant infrastructure, while Firestore removes this concern securing the replication no matter what. This means that, according to Google’s SLAs, the data will continue to migrate from one region to another even if the server holding it fails because their infrastructure has several redundant nodes to secure this process all the way. While Firestore only distributes these nodes within the same continent, MongoDB distributes them globally. An application with a global user base would benefit from MongoDB’s cross-region platform support and worldwide data replication.

4. You need to scale across multiple platforms

Whenever you are using a NoSQL database as your application’s main data store, MongoDB should be your first option; it’s highly scalable in the long run, and even though Firestore has scalability features as well, Firesore was designed to start developing applications quickly and solve problems on the go. MongoDB, on the other hand, can be structured to satisfy immediate, mid-term, and long-term needs as a project matures. Additionally, the Firebase suite is focused on providing a fast ecosystem for mobile development, with crash analytics, performance insights, and push notifications, but in a single region established from the beginning of the project. MongoDB can be used from any ecosystem and has tools to integrate business analytics and structured queries, and its unique protocol to establish connections and be cloned among different regions using the Atlas ecosystem which provides such functionality. 

Conclusion

Here at FullStack Labs, our UI/UX designers, React developers, and Node.js. developers all follow a playbook and use tools such as MongoDB and Firestore. Because our developers are so familiar with JavaScript and its various frameworks, they can easily incorporate particularly JS-friendly databases like Mongo and Firebase.

Frequently Asked Questions