mongodb的model名稱細節

最近研究api設計,順便研究了下mongodb,教程沒有仔細看過,所以使用過程中也遇到了一些詭異的現象。

比如我使用中發現,我創建的模型名稱,在對應數據庫的collections內的名稱不一致。我很納悶,比如我創建的如下:

const PersonModel = Mongoose.model("person", {
  firstname: String,
  lastname: String
});

當我將一條數據寫入後,用工具Robo 3T發現,名稱居然變成了people。

後來查了相關資料,原來mongodb有自己的一套規則,詳細的規則,比如我這條:mongoose/lib/utils.js。當然這個是歷史版本的例子了。關於這個現象,最新文檔中也指出:

The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural version of your model name. For example, if you use

const MyModel = mongoose.model('Ticket', mySchema);

Then Mongoose will create the model for your tickets collection, not your ticket collection.

一般情況他會創建一個複數的model,這種person算特殊的了。所以你寫得model不一定在查詢的時候會一樣,即便不一樣也不要驚訝哦~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章