[英]What does mongoose.Schema return in mongoose?
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const modelSchema = new Schema({
a: String,
b: Date
});
I understand that the first line returns a mongoose. 我知道第一行会返回猫鼬。 But what exactly does mongoose.Schema return in this code? 但是mongoose.Schema在此代码中究竟返回什么? Why do we need it to write the third line, "const modelSchema = new Schema(...)"? 为什么我们需要它来编写第三行“ const modelSchema = new Schema(...)”?
You do not have to. 你不必。
It is just a shortcut for saving time writing all the time mongoose.Schema
... the above code: 这只是节省所有时间编写mongoose.Schema
的捷径...上面的代码:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const modelSchema = new Schema({
a: String,
b: Date
});
Is the equivalent to: 等效于:
const mongoose = require('mongoose');
const modelSchema = new mongoose.Schema({
a: String,
b: Date
});
So to answer your question the line which just gets a reference to the mongoose.Schema
is nothing more than a shortcut
to save yourself writing extra mongoose.
因此,要回答您的问题,仅获得对mongoose.Schema
的引用的行仅是节省您编写额外的mongoose.
的shortcut
mongoose.
every time :). 每次 :)。
The main reason this is often used is since a lot of examples are given with more than one schema
defined in a file/example. 经常使用它的主要原因是,由于许多示例都在文件/示例中定义了多个schema
。 So to save time and not to repeat the same property path every time it is just referenced with a variable Schema
. 因此,为了节省时间,而不必每次使用变量Schema
引用它时都重复相同的属性路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.