Core MongoDB Concepts
/ core-mongodb-concepts
A Basic Introduction
MongoDB Documents
One of these types is Document NoSQL databases.
Document databases are probably the closest thing to relational databases of all the databases in the NoSQL ecosystem.
Documents databases store documents in collections, a lot like relational databases store rows in tables; but, we’ll get more into that later.
One of the most popular representatives of Document databases and one of the leading NoSQL databases, is MongoDB.
A Basic Introduction:
Before we see all the different ways that MongoDB can be used, let’s go through some of the basic concepts of this database.
Guys from MongoDB are very proud of what they call Nexus Architecture. This architecture gives the ability to combine proven concepts and abilities of relational databases with NoSQL innovations.
MongoDB Documents:
MongoDB is a document-oriented database and, as previously mentioned, it has certain similarities to relational databases.
Instead of rows, MongoDB has documents. Unlike relational databases, where information for a given record is spread across many tables, all of the data for a given record is stored within a single document.
Collections in MongoDB
This lesson delves further into the concept of documents and collections; it also goes over the differences between MongoDB and Relational Databases.
We’ll cover the following
What are Collections?
Example
Differences Between MongoDB & Relational Databases
In the previous lesson, we discussed documents briefly. Now let’s get into the details.
What are Collections?:
Documents are stored inside of collections.
Collections are groups of somehow related documents, but these documents don’t need to have the same structure.
Here lies one of the biggest benefits of MongoDB: developers don’t need to know the schema of the database beforehand but can modify the schema, dynamically, during development. This is especially great in systems where we can’t get the schema quite right in the beginning, or there are plenty of edge cases to cover.
Also, this way, the entire problem with impedance mismatch is avoided (i.e., elimination of the object-relational mapping layer).
What does this look like?
Well, let’s say that the previous document is stored in the collection called users; we could add another document into that collection which would contain fields that the previous document didn’t have, or we could add a document that may not have the fields that the previous document had.
Differences Between MongoDB & Relational Databases:
It’s important to emphasize some of the differences between MongoDB and Relational databases.
Firstly, MongoDB doesn’t have foreign keys; but, it has a feature that looks quite like that — References.
Any object can have a reference to some other object, using its id, but this is not automatically updated, and it’s up to the application to keep track of these connections.
This is done in this way because once a foreign key is introduced in a relational database, it can be hard to unwind the database from it.
Thanks to the document data model, and due to the fact that all the necessary information for one “record” is stored inside one document, joins are not provided in MongoDB. However, a similar mechanism called Lookup is available.
Among other differences, it should be mentioned that there is no equivalent of multiple-table transactions in MongoDB.
Command Operations in MongoDB
This lesson introduces all of the basic Mongo Shell command operations that can be performed (e.g., creating, reading, updating and deleting from a database).
We’ll cover the following
CRUD operations from the shell
Using mongo
Creating a DatabaseExample
Using db.help()Example
Collection Operations
Creating a Collection
Showing Collections
Dropping CollectionsExample
Inserting a Document Example
Querying DocumentsExample
Updating a Document:Example
Removing a Document:Example
Indexes:Creating an Index
CRUD operations from the shell
MongoDB Compass