![](/img/trans.png)
[英]What is the difference between mongoose.Schema() and new mongoose.Schema()?
[英]What does mongoose.Schema return in mongoose?
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
});
因此,要回答您的问题,仅获得对mongoose.Schema
的引用的行仅是节省您编写额外的mongoose.
的shortcut
mongoose.
每次 :)。
经常使用它的主要原因是,由于许多示例都在文件/示例中定义了多个schema
。 因此,为了节省时间,而不必每次使用变量Schema
引用它时都重复相同的属性路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.