Schematics of the Cosmos: MongoDB Schemas and Data Operations
Topic Overview and Actualization
Today, we're embarking on a journey into MongoDB. We'll be unveiling Schemas, Models, and Data Read Operations. In simpler terms, Schemas form the database structure, Models generate data, and Data Read Operations fetch it. So, let's go!
Introduction to Schemas in MongoDB
A Schema in MongoDB is akin to an architectural blueprint. In our analogy, it functions as the layout for organizing books in a library. A Schema consists of:
- Fields (individual pieces of data)
- Types (the kind of data for each field)
- Validators (functions that ensure the data meets certain criteria)
Creating a Simple Schema in MongoDB
Let's create a Schema in which the fields represent different book categories.
We've generated a Book Schema with title, author, and isbn. MongoDB comes packed with built-in validation options to maintain data consistency.
We then export the Book model, which other files can access via "Book".
Understanding MongoDB Data Models
A Model in MongoDB represents a schema — the individual books in our library, to be precise.
We've created a Book model, similar to placing a book on a library shelf.
We first import the bookSchema via const Book = require('./bookSchema');
We can now create a new instance of Book, that follows the requirements of
bookSchema.
You may have noticed that initialDBSetup(); is called in the practices. This
function creates the schema and populates the collections of our database.
Introduction to Data Read Operations
Reading data yields matches — much like finding a book in our library. MongoDB offers methods such as find() and findOne() for this purpose.
