MongoDB Academy · 강의

Mongoose 스키마, 모델 및 가상 속성

학습자는 유형, 유효성 검사, 기본 옵션을 포함한 Mongoose 스키마를 정의하고 모델을 만들며 가상 속성을 추가합니다.

레슨 2/413개 단계

Mongoose 스키마, 모델 및 가상 속성은(는) CoddyKit의 무료 MongoDB Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 MongoDB Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Is Mongoose?

Mongoose is an Object Document Mapper (ODM) for MongoDB and Node.js. It sits on top of the official MongoDB driver and adds a layer of abstraction: schemas for validating document structure, models for database interaction, middleware hooks for pre/post operation logic, and virtuals for computed fields. Mongoose is the most popular MongoDB library in the Node.js ecosystem and is especially productive for server applications with well-defined data models.

// Install Mongoose
// npm install mongoose

const mongoose = require('mongoose');

// Connect to MongoDB
await mongoose.connect(process.env.MONGODB_URI);
console.log('Mongoose connected to MongoDB');

Defining a Schema

A Mongoose Schema defines the structure, types, and constraints of documents in a collection. Each key in the schema corresponds to a document field. You can specify type, required, default, min, max, enum, and many other validation options per field. Schemas are the single source of truth for your document structure in a Mongoose application.

const { Schema } = mongoose;

const userSchema = new Schema({
  name: {
    type: String,
    required: [true, 'Name is required'],
    trim: true,
    maxlength: 100
  },
  email: {
    type: String,
    required: true,
    unique: true,
    lowercase: true  // automatically converts to lowercase before saving
  },
  age: {
    type: Number,
    min: [0, 'Age cannot be negative'],
    max: 150
  },
  role: {
    type: String,
    enum: ['admin', 'user', 'guest'],
    default: 'user'
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

Schema Types: Supported Types

Mongoose supports a rich set of schema types. The most common are String, Number, Date, Boolean, Buffer, mongoose.Schema.Types.ObjectId (for references), Array (denoted as [Type]), and Mixed (any value, no type checking). Mongoose also supports nested schemas (sub-documents) by nesting a Schema definition inside another Schema's field definition.

const { Schema } = mongoose;
const { ObjectId } = Schema.Types;

const orderSchema = new Schema({
  customerId: { type: ObjectId, ref: 'User', required: true }, // reference to User model
  items: [
    {
      productId: { type: ObjectId, ref: 'Product' },
      quantity: { type: Number, min: 1 },
      price: Number
    }
  ],
  total: Number,
  status: { type: String, default: 'pending' },
  metadata: Schema.Types.Mixed,  // accepts any shape
  tags: [String],                // array of strings
  shippedAt: Date
});

Timestamps Option

Passing { timestamps: true } as the second argument to the Schema constructor makes Mongoose automatically manage createdAt and updatedAt fields on every document. createdAt is set once on insertion, and updatedAt is updated on every save. You do not need to set these fields manually—Mongoose handles them transparently. This is a best practice for all production schemas.

const productSchema = new Schema(
  {
    name: { type: String, required: true },
    price: { type: Number, required: true },
    category: String,
    stock: { type: Number, default: 0 }
  },
  { timestamps: true }  // adds createdAt and updatedAt automatically
);

// Documents will have:
// { name: 'Laptop', price: 999, createdAt: Date, updatedAt: Date }

Creating a Model

A Model is a constructor compiled from a Schema. It provides the interface for querying and writing documents to the collection. Call mongoose.model('ModelName', schema) to create a model—the first argument is the singular name of the collection (Mongoose automatically pluralizes it: 'User' → 'users' collection). Models should be created once and exported as modules.

// Define schema
const userSchema = new mongoose.Schema({
  name: String,
  email: { type: String, unique: true },
  role: { type: String, default: 'user' }
}, { timestamps: true });

// Compile the model
const User = mongoose.model('User', userSchema);
// This creates/uses the 'users' collection

module.exports = User;

// Usage in another file:
// const User = require('./models/user');
// const user = await User.findOne({ email: 'alice@example.com' });

Creating Documents With new Model()

Create a new document instance using the model constructor: new User({ name: '...', ... }). This creates an in-memory document object with validation but does NOT save to the database. Call .save() on the instance to persist it, or use the static User.create() shorthand that combines both steps. Mongoose validates the document against the schema before saving and throws a ValidationError if constraints are violated.

// Method 1: new + save (two-step)
const user = new User({
  name: 'Alice',
  email: 'alice@example.com',
  role: 'admin'
});
await user.save(); // validates then saves to 'users' collection

// Method 2: User.create() shorthand
const user2 = await User.create({
  name: 'Bob',
  email: 'bob@example.com'
});
console.log('Created user ID:', user2._id);

// Method 3: insertMany for bulk
await User.insertMany([
  { name: 'Carol', email: 'carol@example.com' },
  { name: 'Dave', email: 'dave@example.com' }
]);

Virtual Properties

Virtuals are computed properties that are not stored in the database but are computed from other fields on the document. They behave like regular document fields in your application code but are never written to MongoDB. Common use cases: combining firstName and lastName into a fullName virtual, computing age from a birthDate field, or creating a public-facing url from an _id.

const personSchema = new mongoose.Schema({
  firstName: String,
  lastName: String,
  birthDate: Date
});

// Virtual: combines firstName and lastName
personSchema.virtual('fullName').get(function () {
  return this.firstName + ' ' + this.lastName;
  // Use regular function (not arrow function) to access 'this'
});

// Virtual with a setter for convenience
personSchema.virtual('fullName').get(function () {
  return this.firstName + ' ' + this.lastName;
}).set(function (v) {
  this.firstName = v.split(' ')[0];
  this.lastName = v.split(' ')[1];
});

const Person = mongoose.model('Person', personSchema);
const p = new Person({ firstName: 'Alice', lastName: 'Smith' });
console.log(p.fullName); // 'Alice Smith'

Including Virtuals in JSON Output

By default, virtuals are not included when converting a document to JSON (e.g., when sending it in an API response). To include them, either set { toJSON: { virtuals: true } } in the schema options or explicitly call doc.toJSON({ virtuals: true }). In Express apps, res.json(doc) calls toJSON() automatically, so setting toJSON: { virtuals: true } in the schema is the cleanest way to always include them.

const userSchema = new mongoose.Schema(
  {
    firstName: String,
    lastName: String
  },
  {
    toJSON: { virtuals: true },    // include virtuals in res.json()
    toObject: { virtuals: true }   // include virtuals in .toObject()
  }
);

userSchema.virtual('fullName').get(function () {
  return this.firstName + ' ' + this.lastName;
});

const user = new User({ firstName: 'Alice', lastName: 'Smith' });
console.log(JSON.stringify(user)); // includes 'fullName': 'Alice Smith'

Custom Validation in Schemas

Mongoose schemas support custom validator functions per field. The validator function receives the field value and must return true for valid or false (or throw an error) for invalid. You can also provide a custom error message. Custom validators run before .save() and can be asynchronous (useful for database-level uniqueness checks beyond the unique index).

const productSchema = new mongoose.Schema({
  name: String,
  price: {
    type: Number,
    required: true,
    validate: {
      validator: function (v) {
        return v > 0; // price must be positive
      },
      message: props => 'Price must be positive, got ' + props.value
    }
  },
  sku: {
    type: String,
    validate: {
      validator: function (v) {
        return /^[A-Z]{2}-\d{4}$/.test(v); // format: AB-1234
      },
      message: 'SKU must match format AB-1234'
    }
  }
});

Schema Methods and Statics

Schemas support instance methods (available on each document) and static methods (called on the Model class). Instance methods access this for the specific document, making them ideal for document-specific operations like comparing passwords or formatting output. Statics are useful for common queries or factory functions that don't operate on a specific instance.

const userSchema = new mongoose.Schema({ email: String, passwordHash: String });

// Instance method: available on each user document
userSchema.methods.checkPassword = function (candidatePassword) {
  return bcrypt.compare(candidatePassword, this.passwordHash);
};

// Static method: called on the User model
userSchema.statics.findByEmail = function (email) {
  return this.findOne({ email: email.toLowerCase() });
};

const User = mongoose.model('User', userSchema);

// Usage:
const user = await User.findByEmail('alice@example.com');  // static
const valid = await user.checkPassword('secret');          // instance method

Subdocuments vs Nested Schema Objects

Mongoose distinguishes between embedded subdocuments (defined as an array of schemas) and nested schema objects (a plain schema definition inside a field). Array subdocuments each get their own _id and can be manipulated as individual documents via doc.items.id(subId). Nested objects share the parent document's lifecycle. Use array subdocuments for ordered collections of records (order line items, comments). Use nested objects for one-to-one embedded structures (address, metadata).

// Nested object — no array, no _id per entry
const userSchema = new Schema({
  address: {
    street: String,
    city: String,
    zip: String
  }
});

// Array subdocuments — each item gets its own _id
const orderSchema = new Schema({
  items: [
    {
      productId: Schema.Types.ObjectId,
      quantity: Number,
      price: Number
      // _id auto-added to each item
    }
  ]
});

// Access subdocument by ID:
const item = order.items.id(someItemId);

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: Mongoose Schemas define document structure, types, and validation rules, Models are compiled from schemas and provide the query/write interface (Model.find(), new Model(), etc.), and virtuals are computed properties that exist in memory but are never stored in MongoDB — useful for derived fields like fullName or url. Next up we explore Mongoose's query API including chaining, .lean(), and comparison with the native driver.

무료로 시작

AI 튜터와 함께 JavaScript을(를) 배우세요 — 무료

브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.

코스
30
레슨
120

자주 묻는 질문

“Mongoose 스키마, 모델 및 가상 속성” 강의는 무료인가요?

네 — “Mongoose 스키마, 모델 및 가상 속성” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 MongoDB Academy 강의 전체를 잠금 해제할 수 있습니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“Mongoose 스키마, 모델 및 가상 속성”에서 뭘 배우나요?

학습자는 유형, 유효성 검사, 기본 옵션을 포함한 Mongoose 스키마를 정의하고 모델을 만들며 가상 속성을 추가합니다. 브라우저에서 직접 실행하는 실습 코드로 MongoDB Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

MongoDB Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 MongoDB Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“Mongoose 스키마, 모델 및 가상 속성” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 MongoDB Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 MongoDB Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 공식 Node.js 드라이버로 연결하기
  2. Mongoose 스키마, 모델 및 가상 속성
  3. Mongoose 쿼리, 연결 및 Lean 문서
  4. Mongoose 미들웨어: 사전 및 사후 훅
← MongoDB Academy(으)로 돌아가기