# 🏅 Day 7 - Model、Schema Options
### Model
建立 Model 連接資料庫中的 Collection 及 Schema,
就可以使用 Model 對 Collection 做資料相關處理(CRUD)
語法範例:
```javascript
// Model 名稱會使用大寫開頭
const CollectionModel = mongoose.model('Collection', collectionSchema);
```
> `mongoose.model()` 第一個參數為 Collection 的名稱,第二個參數帶入 Schema
> 其中需注意第一個參數 Collection 的名稱(`'Collection'`),Mongoose 會自動視為小寫開頭及改為複數(結尾加 s)
因此 `CollectionModel` 實際上是連接至名稱為 `collections` 的 Collection
### Schema Options
建立 Schema 時,Mongoose 也有提供一些可調整的設定
```javascript
const collectionSchema = new mongoose.Schema({..}, options);
```
在 `mongoose.Schema()` 中第一個參數放 Schema,第二個參數可以調整設定
以下介紹幾個設定,詳細可參考[文件說明](https://mongoosejs.com/docs/guide.html#options)查看所有可調整的 options
#### collection
Mongoose 預設會將 model 連接的 collection 的名稱轉為全小寫,並以複數呈現。以上方建立 Model 的範例語法為例:`'Collection'` 會被視為 `collections`。
若是需要固定的 collection 名稱,可以在 Schema 設定 `{ collection: '...' })`
範例:
```javascript
const collectionSchema = new mongoose.Schema(
{..},
{ collection: 'testCollection' }
);
```
上方設定會將 collection 固定為 `testCollection`。
此時若是新增 Model 時設定不同的 collection 名稱連結該 Schema:
`const testModel = mongoose.model('Test', collectionSchema);`
仍然會以 `{ collection: 'testCollection' }` 的設定為主,連接至 `testCollection` Collection
#### versionKey
新增 document 時,預設都會在 document 中加上 `__v: 0`,若不需要加上此設定的話,可以設定 `versionKey: false` 將 versionKey 移除
```javascript
const collectionSchema = new mongoose.Schema(
{..},
{ versionKey: false }
);
```
#### timestamps
在 Schema 設定 timestamps,在新增資料時會自動加上 `createdAt` 和 `updatedAt` 欄位
```javascript
const collectionSchema = new mongoose.Schema(
{..},
{ timestamps: true }
);
```
### 參考資源
- [Mongoose: Schemas](https://mongoosejs.com/docs/guide.html#options)
- [Mongoose: SchemaTypes](https://mongoosejs.com/docs/schematypes.html)
- 課程章節「Mongoose NPM 教學」
題目
---
1. 延續前一天的 Schema 範例(訂房資訊 `orderSchema`),並根據以下需求調整與:
- 移除 createdAt 改為使用 timestamps,讓資料新增時自動帶入 `createdAt` 和 `updatedAt` 欄位
- 設定 collection 為 `orderCollection`
```javascript
const orderSchema = new mongoose.Schema({
/* 填入 Schema */
}, {
/* 填入 options */
});
```
2. 建立 Model 連接 `orderCollection` 與 `orderSchema`
## 回報流程
將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔!
解答位置請參考下圖(需打開程式碼的部分觀看)

<!-- 解答:
const orderSchema = new mongoose.Schema({
user: {
type: String,
required: [true, 'user is required.'],
select: false
},
userTel: {
type: String,
required: [true, 'userTel is required.'],
select: false
},
room: {
type: String,
required: [true, 'room is required.'],
enum: ['單人房','雙人房','四人房']
},
isPaid: {
type: Boolean,
default: false
}
}, {
collection: 'orderCollection',
timestamps: true,
});
const Order = mongoose.model('orderCollection', orderSchema);
// Model 名稱可以自行取名,但是需使用大寫開頭
-->
回報區
---
<!--
將答案貼至下方表格內,格式:
| Discord 暱稱 | [CodePen](連結) |
-->
| Discord | CodePen / 答案 |
|:-------:|:---------------:|
| xxx | [CodePen]() |
| 圈圈 | [CodePen](https://codepen.io/wjejfczn-the-bold/pen/yLrEvLv?editors=0010) |
| 羽 | [CodePen](https://codepen.io/lingling-Syu/pen/dyLKJBB?editors=0010) |
| 苡安 | [hackmd](https://hackmd.io/@L7K9-66lSeagS28AP0_GjQ/S1GA3GIgA) |
| Lobinda | [CodePen](https://codepen.io/Lobinda/pen/LYvrQWP) |
| YC | [CodePen](https://codepen.io/YCLu/pen/XWQEzKp) |
| 群嘉 | [CodePen](https://codepen.io/efzdamnp-the-lessful/pen/LYvrQJp?editors=0010)|
| Jean | [CodePen](https://codepen.io/jeanchiang1221/pen/gOyKvEG) |
| shuantt | [CodePen](https://codepen.io/th-tseng/pen/MWRXVYd)
| Otis | [CodePen](https://codepen.io/humming74/pen/oNOydWP)
| Chia Pin | [CodePen](https://codepen.io/joker-cat/pen/KKYeeeB)
| wei | [CodePen](https://codepen.io/Wei-the-sasster/pen/YzMvvOG?editors=0010)
| Tiya | [CodePen](https://codepen.io/Tiya_blank/pen/JjVZaWv)
| 阿旭 | [CodePen](https://codepen.io/koei-lu/pen/rNbKZdY)
|cho|[CodePen](https://codepen.io/cho195/pen/GRLGYJo)|
|kelvinnn|[CodePen](https://codepen.io/is-lin/pen/XWQYxLv)|
|ej_chuang|[CodePen](https://codepen.io/EJChuang/pen/dyLKwye)|
|william_hsu|[CodePen](https://codepen.io/william8815/pen/oNOyQVp)|
|tung030374 |[CodePen](https://codepen.io/tung__u/pen/mdgKaab)|
| Theodore | [CodePen](https://codepen.io/GustavoFringgg/pen/vYMrbjK?editors=0010) |
| Thomas | [CodePen](https://codepen.io/ThomasYeh/pen/abxjbMJ) |
| william威良 | [CodePen](https://codepen.io/snowman12320/pen/GRLBJpp?editors=1010) |
| runweiting | [CodePen](https://codepen.io/weiting14/pen/wvZxGye) |
| Hank | [CodePen](https://codepen.io/tw1720/pen/RwOJMKx) |
| Aida | [CodePen](https://codepen.io/ada23410/pen/gOyjmNb?editors=0010) |
| mei | [CodePen](https://codepen.io/l_umei/pen/rNbrmgb) |
| Fabio20 | [CodePen]([連結](https://codepen.io/fabio7621/pen/VwNBBRr?editors=0010)) |
| jenny7532| [CodePen](https://codepen.io/wei-chen-wu/pen/OJGwaOv) |
| 2魚 | [CodePen](https://codepen.io/ijrekmsn-the-sans/pen/wvZxNzW) |
| Henry | [Codepen](https://codepen.io/hekman1122/pen/eYojqgd?editors=0010) |
| Benson | [Codepen](https://codepen.io/nosneb83/pen/vYMzgWx) |
| zaoannihao | [CodePen](https://codepen.io/ckhwdvrx-the-solid/pen/poBOwBm) |
| blp100x | [CodePen](https://codepen.io/blp100/pen/jORvooa) |
| ellallu0903 | [CodePen](https://codepen.io/ellallu0903/pen/RwOqbmb) |
| yoshidc | [CodePen](https://codepen.io/yoshiyyc/pen/jORQQxY) |
| Cile | [CodePen](https://codepen.io/nycteachen/pen/GRLweXq) |
| 瑀君 | [CodePen](https://codepen.io/yennnnn/pen/ZEZVpNE)|
| albertyang3576 | [CodePen](https://codepen.io/albertyang3576/pen/NWmmRKx?editors=0010)|
| puffy_chen | [CodePen](https://codepen.io/TernMayDay/pen/VwOvRzB?editors=1010)