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? Why do we need it to write the third line, "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:
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.
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. So to save time and not to repeat the same property path every time it is just referenced with a variable Schema
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.