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.

const mongoose = require('mongoose');
const { Schema } = mongoose;

const bookSchema = new Schema({
  title:  String,
  author: String,
  isbn:   Number
});

module.exports = mongoose.model('Book', bookSchema);

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.

const mongoose = require('mongoose');
const Book = require('./bookSchema');
let newBook = new Book({
    title: "Eloquent JavaScript",
    author: "Marijn Haverbeke",
    isbn: 9781593279509
});

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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal