Skip to main content

Command Palette

Search for a command to run...

[Node] Database and MongoDB

04/15/23

Updated
[Node] Database and MongoDB

What is Database?

  • Software designed to efficiently store and retrieve data is called a Database Management System (DBMS).

  • The server computer where the DBMS is installed can be referred to as the Database Server (DB Server).

  • When people commonly say "data is stored in a database," they are referring to the fact that the data is managed by the DBMS on the DB Server.

Two types of DB

  • Relational Database (RDB): A type of database that focuses on maintaining data with defined formats and relationships among data to ensure consistency and integrity of data is called a relational database.

    • Consistency and integrity of data: data with high integrity and consistency.
  • Non-relational Database (NoSQL): Any type of database that does not belong to the relational database category is referred to as a non-relational database or NoSQL database. Non-relational databases are flexible in terms of data format and can be scaled easily, but if data is not properly managed, the data stored in the database may become unreliable.

    • This type of database is often adopted by many startups for its flexibility in design.

What is MongoDB?

  • One of the most popular NoSQL databases used by many developers domestically and internationally.

  • All data is stored in JSON format.

  • It has the advantage of easily storing complex structures.

  • It is available for free to use.

  • It can easily scale up or down.

Relationship between Web Server and DB Server

  • The web server provides the data and functionalities requested by the web client.

  • The DB server stores data for optimal performance and provides the data requested by the DB client.

  • Ultimately, the two servers differ only in what they provide, but their basic principles are similar.

MongoDB

MongoDB Client - Studio 3T

  • Studio 3T is a MongoDB client designed to assist with using MongoDB, similar to an API client that helps with API usage.

  • Studio 3T is a program that provides an easy-to-use GUI for managing data stored in MongoDB, displaying it in a user-friendly manner.

CRUD Command in MongoDB

  1. find: db.collectionName.find({})

  2. insert one: db.collectionName.insertOne({ key: "value", key2: "값" })

  3. delete one: db.collectionName.deleteOne({ _id: ObjectId("...")})

  4. update one: db.collectionName.updateOne({ _id: ObjectId("...")}, {$set : {attribute}})

Mongoose

Document in mongoose

  • A document in mongoose refers to each individual piece of data that is stored in MongoDB.

  • It consists of one or more key-value pairs, in JSON format.

{
    "_id": ObjectId("6682192a1c155bd2f27881"),
    "name": "lyw",
}

Collection in mongoose

  • A collection in mongoose is similar to a table in relational databases and can hold multiple documents in JSON format.

Schema in mongoose

  • A schema in mongoose defines what type of values can be included in the documents of a collection.

  • It is used for modeling data and specifies the structure of the data.

  • ❓ Some common types in a schema are:

    • null: for null values or non-existent fields

      • ex: null
    • String: for strings

      • ex: "mongoDB"
    • Number: for numbers

      • ex: 3.14
    • Date: for dates

      • ex: new Date()
    • Buffer: for buffers that can hold files or non-UTF-8 encoded strings

      • ex: 0x65
    • Boolean: for true or false

      • ex: true
    • ObjectId(Schema.Types.ObjectId): for object IDs, often used for referencing other objects

      • ex: ObjectId()
    • Array: for arrays of values

      • ex: ["a", "b", "c"]

Model in mongoose

  • A model in mongoose is responsible for defining the structure of data that will be stored in the database.

  • It is created using a schema and provides functions to perform operations on MongoDB.

  • It is used to create documents when storing data.