javascript - mongoose.Schema 在猫鼬中返回什么?
问题描述
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const modelSchema = new Schema({
a: String,
b: Date
});
我知道第一行返回一只猫鼬。但是 mongoose.Schema 在这段代码中究竟返回了什么?为什么我们需要它来写第三行,“const modelSchema = new Schema(...)”?
解决方案
你不必。
它只是节省时间写作的捷径mongoose.Schema
......上面的代码:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const modelSchema = new Schema({
a: String,
b: Date
});
相当于:
const mongoose = require('mongoose');
const modelSchema = new mongoose.Schema({
a: String,
b: Date
});
因此,要回答您的问题,仅引用 the 的行mongoose.Schema
无非是shortcut
为了节省mongoose.
您每次都写额外的东西:)。
经常使用它的主要原因是因为很多示例都schema
在文件/示例中定义了多个示例。因此,为了节省时间,并且每次仅使用变量引用它时都不要重复相同的属性路径Schema
。
推荐阅读
- sql-server - 安装 CU14 后,SQL Server 为父节点返回不正确的 XML 值
- jquery - 如何从预先确定的订单中订购控件
- python-requests - Python 客户端证书请求在 Ubuntu 上失败
- forms - 如何在 Xamarin Form 的 Grouped ListView 中创建页脚模板
- java - 通过 Jython (Python) 调用 Java 以通过 Anisble Python API 运行 Ansible 脚本
- python - 分组后如何将行转换为列(具有自定义名称)?
- android - 底部工作表对话框片段隐藏并在从后台返回后显示
- java - 是否可以强制 SSLHandshake 始终使用主机名,而不是 HttpsUrlConnection 的 IP
- docker - 使用 Jenkins Job DSL 创建/修改 Docker 代理模板
- r - 使用 for 循环从模型集合中绘制变量重要性