MongoDB 101: For beginners (Part?1)
Table of?contents
1- What is?MongoDB?
MongoDB is?an open-source document-oriented?NoSQL?database, that was founded in 2007 by?Dwight Merriman, Eliot Horowitz, and Kevin Ryan?— the team behind DoubleClick.?MongoDB now has a huge community of developers who loved it for its high performance, availability, and scalability, while being simple and easy to learn.
If you’re already familiar with RDBs (Relational?DataBases) such as MySQL, you probably already know what tables, columns, and rows mean. However, things with MongoDB are a little different. MongoDB is built on the concept of collections and JSON-like documents instead of tables, rows, and columns. I will explain what is collections and documents in the next sections.
2- MongoDB?Document
Each record in a MongoDB database is called a Document and it is analogous to a row in a SQL table. MongoDB document has a?JSON-like?structure, If you’re not familiar with JSON you can just think of it as an object of?key-value?pairs. The key represents a?field?and it’s equivalent to a column in Relational Databases, and the value of a field?could be of any type (e.g. string, number, arrays, another object, an array of nested objects, embedded sub-document, etc…).
The example above is a simple example of a customer document. Each document has a unique _id value, it’s basically the primary key that identifies each document in a collection of documents. The MongoDB driver automatically generates an ObjectId for the _id field. But wait what is that ObjectId? Well, according to MongoDB docs:-
"ObjectIds are small, likely unique, fast to generate, and ordered. ObjectId values are 12 bytes in length, consisting of a 4-byte timestamp value, representing the ObjectId’s creation, a 5-byte random value generated once per process, and a 3-byte incrementing counter, initialized to a random value."
3- MongoDB Collection
Now that you understand the basic building unit of any MongoDB database which is the document, it’s time to introduce you to the MongoDB collection. A collection is a set of related documents that makes up a database. Collections act as the equivalent of SQL tables. Below is an example of a “User” collection, which is basically a collection of users. MongoDB has more cool features that I will explain next so bear with me ??.
MongoDB is Schemaless
Unlike the relational database which enforces a predefined schema, MongoDB offers a dynamic schematic architecture that gives the flexibility of storing any type of data in the same collection. This also gives the ability to change the structure of the documents in a collection, such as adding new fields, removing existing fields, or changing the field values to a new type.
This flexibility comes in handy when working with unstructured data such as; big data analysis, machine learning and artificial intelligence operations, and even real-time sensors readings. In practice, however, you can enforce a strict dynamic schema at the application level during an update and insert operations.
4- Data?Modeling
So let's say we have a blogging site, it can have multiple users and each user can write multiple posts, add comments, and likes. The first thing you may think of is how all these data should be stored, and how they relate to each other right? Well, this is called Data Modeling. Data modeling can be challenging because it heavily depends on the business needs with some trade-offs to consider.?
Along with the traditional References technique where a child document references a parent document, MongoDB has a core feature that allows you to store a document inside another document. This feature is called Document Embedding.
Let’s take a look at how we can model blogging site data using the aforementioned technique.
Document Embedding
Embedding documents simply means storing related data in a single document structure instead of storing them in separate collections. This is a denormalizing technique, which results in better performance that allows querying and updating related data in a single database operation. This modeling method is best used when:
Let’s get back to our blogging site example, each post may have some comments right? do we need to view those comments on their own or with the post itself? do we need to frequently run updates on those comments?…. If all answers are No, then the best choice is to embed those comments in the post document itself as shown in the example below. This way we only need one database call to retrieve the post along with all its comments.
Conclusion
To wrap up, after reading this introductory guide you should be familiar now with how MongoDB works, what is a document, what is a collection, and how to model data using the document embedding method. In part 2, we'll learn about CRUD operations in MongoDB so stay tuned, and thank you for reading!
Cultural Development Fund, CCNA | Security +
3 年Thanks! Amany ??????