繁体   English   中英

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM