Databases

Mongoose

About
  • Mongoose .

  • It's a wrapper for MongoDB.

  • Is Mongoose an ORM?

    • Mongoose is not technically an ORM (Object-Relational Mapping), but rather an ODM (Object-Document Mapping).

    • Although both ORM and ODM have similar goals — mapping objects in code to database entities — the difference is the type of database each one handles.

Explanations
Database
  • How to rename a Database .

    • I don't really know yet where the DB lives on the filesystem.

      • There are files inside Program Files/mongodb .

Schemas and Models
  • Creating a Schema and Model:

import mongoose from 'npm:mongoose';

mongoose.connect('mongodb://localhost/meu_banco', { useNewUrlParser: true, useUnifiedTopology: true });

const user_schema = new mongoose.Schema({
  nome: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  idade: { type: Number, min: 18 }
});

const User = mongoose.model('User', user_schema);
  • Collection name:

    • .

Documents
  • Via new Model  and .save

const new_user = new User({
  nome: 'John',
  email: 'john@email.com',
  idade: 25
});

new_user.save()
  .then(() => console.log('User saved!'))
  .catch((err) => console.log('Error saving the user:', err));
  • Via .create :

const new_character = await Character.create({
    nome: 'John',
});
Validations
  • Taken from the video above .

  • A schema created by default has its properties set as 'not required'.

  • .

  • .

  • .

  • .