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, orArray. - Options: Additional settings like
required,default, orunique.
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
stringinstead ofString. - Forgetting to wrap type and options inside an object when options are needed.
- Not setting
requiredordefaultproperly, 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
| Type | Description | Example |
|---|---|---|
| String | Text data | { name: String } |
| Number | Numeric data | { age: Number } |
| Date | Date and time | { createdAt: Date } |
| Boolean | True or false | { isActive: Boolean } |
| Array | List of values | { tags: [String] } |
| ObjectId | Reference 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.