Mongoose Schema 字段类型
在 Mongoose 中,Schema
用于定义 MongoDB 文档的结构,支持多种数据类型。
1. 基本数据类型
类型 | 说明 | 示例 |
---|---|---|
String |
字符串 | name: { type: String } |
Number |
数字(整数或浮点数) | age: { type: Number } |
Boolean |
布尔值 | isActive: { type: Boolean } |
Date |
日期 | createdAt: { type: Date } |
Buffer |
二进制数据(如文件) | file: { type: Buffer } |
ObjectId |
MongoDB 的 _id 字段 |
userId: { type: mongoose.Schema.Types.ObjectId } |
Array |
数组 | tags: [String] |
Mixed |
任意类型(无类型检查) | metadata: { type: mongoose.Schema.Types.Mixed } |
2. 特殊类型
类型 | 说明 | 示例 |
---|---|---|
Decimal128 |
高精度浮点数 | price: { type: mongoose.Schema.Types.Decimal128 } |
Map |
键值对(ES6 Map ) |
attributes: { type: Map, of: String } |
Schema.Types.ObjectId |
引用其他文档 | author: { type: Schema.Types.ObjectId, ref: 'User' } |
3. 嵌套 Schema
可以定义嵌套的子文档结构:
1 | const addressSchema = new mongoose.Schema({ |
4. 数组类型
可以定义数组,并指定数组元素的类型:
1 | const postSchema = new mongoose.Schema({ |
5. 自定义验证
可以添加验证规则:
1 | const userSchema = new mongoose.Schema({ |
6. 默认值和自动生成
可以设置默认值或自动生成字段:
1 | const postSchema = new mongoose.Schema({ |
总结
Mongoose 提供了丰富的字段类型,包括:
• 基本类型:String
, Number
, Boolean
, Date
, Buffer
, Array
, ObjectId
, Mixed
• 特殊类型:Decimal128
, Map
• 嵌套 Schema 和 数组类型
• 自定义验证(required
, min
, max
, match
)
• 默认值 和 自动生成字段