NoSQL is a term that we have become very familiar with in recent times and it is used to describe a set of databases that don't make use of SQL when writing & composing queries. There are loads of different types of NoSQL databases ranging from key-value databases like the Reddis to document-oriented databases like MongoDB and Firestore to graph databases like Neo4J to multi-paradigm databases like FaunaDB and Cassandra.
I believe that you might have worked with at least one or two of these types of databases in your career and if you by chance have not gotten to work on them make sure you finish the article because by the time we are done, you'll have a sense of direction and which one of them you should start with. I will not go into detail or discuss all the different types of NoSQL databases rather I'm going to focus on only document-oriented databases as they are the most popular in this category.
We are going to cover the following in this post as we discuss the best types of document-oriented NoSQL databases out there.
- What are document-oriented databases
- Firebase Firestore
- Which one should you go for?
Document Oriented databases are a type of NoSQL database where data is stored in JSON-like documents, A document will store the fields for the data and the value for that field. Multiple documents are stored inside a collection. Document-oriented databases have the advantage of being optionally schema-less. This implies that you don't have to define a strict structure for the documents in a collection. This allows you to accurately model data with a loosely defined structure.
Document-oriented databases don't make use of a key be it primary or secondary keys rather all documents have an ID Which can be used to retrieve a particular document. You also don't have to worry about writing complex joins when querying your database because relationships between different aspects of your data are modeled differently. Let's say we have a User and we are collecting and storing their address. Instead of creating a separate collection for housing, the user's address we can embed it as an array of address objects on a user document so that anytime we retrieve a user we have the addresses associated with their accounts automatically without having to make a second query or any complex joins. This approach also limits the overall number of queries you need to make when trying to fetch data. NoSQL document databases can accommodate different types of documents because they use a flexible schema and most NoSQL document databases are designed with scalability in mind incorporating mechanisms like sharding and active-active clusters. Users can increase the database capacity without needing more hardware.
This is not to say that Document oriented data are not without any drawbacks, the fact that you can create a schemaless database should put you off a bit because since you can't describe a structure for your data, you will start to have documents with inconsistent structure which could lead to bugs in your apps. Second, you cannot accurately model a data set with a strong relationship inside a document-oriented database and sometimes embedding won't give you as much fine-grained control as you'd prefer. Let's look at some of the best NoSQL document-oriented databases.
MongoDB stores data in the form of JSON-like objects, however, MongoDB stores our data using BSON which stands for Binary JSON. BSON is a binary-encoded serialization of JSON documents. BSON is derived from JSON but BSON supports more complex data types like dates and binary data. BSON was created by the MongoDB team specifically for use on MongoDB. BSON is the format used both for data storage and network transfer in MongoDB.
MongoDB supports horizontal scaling, this is achieved by using horizontal partitioning with a shard key, The shard key is selected by the user and it determines how the data in a collection will be distributed. MongoDB handles load balancing across different shrads automatically with very little to no effort on your part.
A cool feature of MongoDB is that it will automatically replicate and distribute copies of our documents stored in each collection. This ensures that there will always be at least two copies of a document, any of which can serve as the primary source for read or write operations. MongoDB will manage the different data sources to ensure that each time we get consistent data when making read or write operations.
Faunadb is a multi-paradigm relational database with a built-in document data model. Faunadb gives developers the benefit of working with relational and document databases inside a cloud API that can be accessed from almost any platform. Faunadb has its query language called FQL (Fauna Query Language) an expression-oriented query language that enables complex, precise manipulation and retrieval of documents. All FQL queries are transactional ensuring data integrity. FaunaDB is Multi-regional and scales up horizontally using active-active clusters while incorporating most modern application security features.
FaunaDB provides developers with references. A reference is a compound value that serves as a unique ID for any document in a collection, A reference is comprised of a reference to the collection containing the document and a document ID which is a number. No two documents in a database can share the same reference.
The FQL provides built-in functions which can be used to run queries. We can compose multiple built-in functions to create User-defined functions (or UDFs) UDFs are used to combine frequently used functions into queries that can be stored and executed repeatedly. FaunaDB allows you to import data into your database from different sources. You can import a JSON file into your database or you can also import a CSV file if that's what you're comfortable with.
Firebase Firestore is a NoSQL document-oriented database from Firebase. A SAS platform owned by Google. Firestore enables rapid application development by providing a database that is relatively simple and easy to use when compared to others on this list. Firestore keeps your data in sync across client apps through real-time listeners and offers offline support for mobile and web. Firestore allows for seamless integration with other Firebase and Google Cloud products.
The Firestore data model supports flexible, hierarchical data structures. You can store your data in documents, which can be organized into collections. In addition, Documents can contain complex nested objects including subcollections.
In Firestore, you can use simple queries to retrieve individual, specific documents or to retrieve all the documents in a collection that matches your query parameters. Your queries can include multiple, chained filters and combine filtering and sorting. They're also indexed by default, so query performance is proportional to the size of your result set, not your data set. Its query language is the simplest of all on this list.
Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even if the device is offline. When the device comes back online, Firestore synchronizes any local changes back to Cloud Firestore.
Well, ultimately it's down to you, your needs and your requirements will dictate which you should go for. If you're looking for something easy to use and integrate, with offline support then the obvious answer is Firestore, however, if you're looking for something robust then you'd have to pick between MongoDB and FaunaDB. FaunaDB has a slight edge over MongoDB because FaunaDB allows you to create a GraphQL API for interacting with your database without having to set anything, Just a few clicks of the button and you have a GraphQL API all set for you, and this is something to consider especially if you're comfortable working with GraphQL.
I'd like to know your thoughts about working with the databases on this post and if you have worked with other NoSQL document-oriented databases then please share your thoughts and experience on working with them in the comments section and I will see you in the next post.