Mongoose 中使用 Schema Types 类型详解
Mongoose 是一个 Node.js 中用于连接 MongoDB 数据库的 Object Modeling 工具,它为 MongoDB 数据库提供了更为简单的操作方式和更严谨的数据结构描述。Schema Types 则是 Mongoose 中定义数据结构的核心部分,它可以让我们以一种非常灵活的方式定义数据类型并且控制存储结构,本文将详细介绍 Mongoose 中 Schema Types 的使用方式。
一、Schema Types 常用类型
在 Mongoose 中,Schema Types 提供了丰富的数据类型,尽管大部分与 MongoDB 原生的数据类型相同,但是 Mongoose 提供了更丰富的选项以及更高效的操作:
- String: 字符串类型,可定义长度、枚举值、正则表达式等
const schema = new mongoose.Schema({ username: { type: String, lowercase: true, trim: true, required: true } })
- Number: 数字类型,可定义最大值、最小值、默认值等
const schema = new mongoose.Schema({ age: { type: Number, min: 0, max: 120, default: 18 } })
- Date: 日期类型,可定义默认值、最大值、最小值等
const schema = new mongoose.Schema({ created_at: { type: Date, default: Date.now } })
- Boolean: 布尔类型,可定义默认值
const schema = new mongoose.Schema({ is_active: { type: Boolean, default: true } })
- Mixed: 混合类型,可存储任意数据类型,但不提供查询支持
const schema = new mongoose.Schema({ data: { type: mongoose.Schema.Types.Mixed } })
- ObjectId: ObjectId 类型,直接映射 MongoDB 中的 ObjectId 或者保存引用
const schema = new mongoose.Schema({ user_id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } })
- Array: 数组类型,可定义数组元素的数据类型
const schema = new mongoose.Schema({ tags: { type: [String] } })
以上是 Mongoose 中常用的数据类型,如果需要更高级的控制,还有 Buffer, Decimal128, Map 等类型可以使用。
二、Schema Types 可选项
除了数据类型之外,Schema Types 还提供了大量可选项,以帮助我们控制数据的存储方式:
- required: 是否必需,默认为 false
const schema = new mongoose.Schema({ username: { type: String, required: true } })
- default: 默认值
const schema = new mongoose.Schema({ created_at: { type: Date, default: Date.now } })
- unique: 是否唯一
const schema = new mongoose.Schema({ username: { type: String, unique: true } })
- index: 是否创建索引
const schema = new mongoose.Schema({ username: { type: String, index: true } })
- validate: 自定义验证函数
-- -------------------- ---- ------- ----- ------ - --- ----------------- ---- - ----- ------- --------- - ---------- ----------- - ------ - - ---- -- -------- ---- ---- -- ---- ---- ---- - - --
以上是 Mongoose 中 Schema Types 常用的选项,通过这些选项,可以对数据进行更为精确的控制。
三、Schema Types 数据查询
Mongoose 中查询 Schema Types 数据十分便捷,它提供了丰富的查询方式:
- 基础查询:通过直接使用查询方法,例如 find、findOne 等方法查询数据
Model.find({ age: { $gt: 18 } }).exec(function(err, users) { console.log(users); });
- 比较查询:与原生 MongoDB 语法相同,使用 $eq、$ne、$gt、$gte、$lt 和 $lte 操作符
-- -------------------- ---- ------- ----- ------ - --- ----------------- ---- - ----- ------- ---- -- ---- --- - --- ------------ ---- - ----- --- ----- -- - --------------------- ------ - ------------------- ---
- 包含查询:使用 $in 和 $nin 操作符
Model.find({ username: { $in: ['Alice', 'Bob'] } }).exec(function(err, users) { console.log(users); });
- 正则表达式查询:通过 $regex 操作符使用正则表达式查询
Model.find({ username: { $regex: /acme.*corp/i } }).exec(function(err, users) { console.log(users); });
以上是 Mongoose 中 Schema Types 的查询方式,查询方式类似于原生 MongoDB 语法,但是通过 Mongoose 的封装,使得查询更为快捷、简单和高效。
总结
通过本文的介绍,相信你已经了解了 Mongoose 中 Schema Types 的基本用法,可以根据自己的需求,灵活地使用 Schema Types 进行数据的定义和操作。同时,也应该依据官方文档了解这些 API 的详细用法,以便更加熟练地使用这个工具。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/649dca6848841e9894a7482f