0
0
MongodbHow-ToBeginner · 3 min read

How to Define Schema Types in Mongoose: Simple Guide

In Mongoose, you define schema types by creating a Schema object where each field has a type like String, Number, or Date. You specify these types inside an object passed to mongoose.Schema to describe the shape of your MongoDB documents.
📐

Syntax

To define schema types in Mongoose, you create a new Schema by passing an object where keys are field names and values specify the type and options.

  • Field name: The name of the property in your document.
  • Type: The data type like String, Number, Date, Boolean, or Array.
  • Options: Additional settings like required, default, or unique.
javascript
const mongoose = require('mongoose');

const schema = new mongoose.Schema({
  fieldName: { type: String, required: true },
  age: Number,
  createdAt: { type: Date, default: Date.now }
});
💻

Example

This example shows how to define a simple user schema with different types and options. It demonstrates string, number, date, and boolean types with required and default values.

javascript
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
  age: Number,
  email: { type: String, unique: true, required: true },
  isActive: { type: Boolean, default: true },
  joinedAt: { type: Date, default: Date.now }
});

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

// Example document creation
const newUser = new User({ name: 'Alice', age: 30, email: 'alice@example.com' });
console.log(newUser);
Output
User { name: 'Alice', age: 30, email: 'alice@example.com', isActive: true, joinedAt: 2024-06-01T00:00:00.000Z }
⚠️

Common Pitfalls

Common mistakes when defining schema types include:

  • Using lowercase type names like string instead of String.
  • Forgetting to wrap type and options inside an object when options are needed.
  • Not setting required or default properly, causing unexpected missing or undefined values.
  • Confusing arrays of types with single types.
javascript
const wrongSchema = new mongoose.Schema({
  name: 'string', // Wrong: should be String
  tags: String[] // Wrong: should be [String]
});

const correctSchema = new mongoose.Schema({
  name: String,
  tags: [String]
});
📊

Quick Reference

TypeDescriptionExample
StringText data{ name: String }
NumberNumeric data{ age: Number }
DateDate and time{ createdAt: Date }
BooleanTrue or false{ isActive: Boolean }
ArrayList of values{ tags: [String] }
ObjectIdReference to another document{ userId: mongoose.Schema.Types.ObjectId }

Key Takeaways

Define schema types in Mongoose by passing an object with field names and their types to mongoose.Schema.
Use capitalized type names like String, Number, Date, Boolean, and Array for correct schema definitions.
Wrap type and options in an object when you need to add settings like required or default.
Arrays of types are defined using square brackets, e.g., [String] for an array of strings.
Common errors include using lowercase types and incorrect array syntax.