Schemas And Models Flashcards
What is a model?
It is the blueprint for a document to be stored in the database, similar to a class.
What is a schema?
It is the rules that the model uses to create its blueprint for creating documents.
How do you create a schema?
const schema = new mongoose.Schema({attr: {type: String/Number/Date, default: val, required: [true, ‘error message’]}, attr: DataType});
What are some other important schema type options?
unique: true/false, trim: true/false
Which JavaScript built-in function gives the current date and time in milliseconds?
Date.now()
How do you set an attribute’s data as an array of data elements in a schema?
attr: [dataType]
How do you create a model?
const ModelName = mongoose.model(‘ModelName’, Schema)
When you create a new model, what happens in the background?
A new collection is created in the database named after the model.