mongoose 拾遺

mongoose 文檔中提到:

When your application starts up, Mongoose automatically calls ensureIndex for each defined index in your schema. Mongoose will call ensureIndex for each index sequentially, and emit an 'index' event on the model when all the ensureIndex calls succeeded or when there was an error. While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact. Disable the behavior by setting the autoIndex option of your schema to false, or globally on the connection by setting the option config.autoIndex to false.

在開發(fā)過程中可以使用 ensureIndex,這樣每次改動數(shù)據(jù)庫結(jié)構(gòu)時,自動生成對應(yīng)的 Index 。而在生產(chǎn)環(huán)境中,因為 ensureIndex 操作會影響性能,所以建議禁止使用。

在沒有理解 MongoDB 的生成 Index 操作前,對上述解釋可能迷惑。因為如果禁止 ensureIndex,那么所有的 Index 不會生成,也就不會起效。但是注意這句話的前提條件是第一次創(chuàng)建 Collection 時。也就是說如果在創(chuàng)建 Collection 時,沒有禁止 ensureIndex 那么相應(yīng)的 Index 就會生成,以后即使禁止了 ensureIndex ,已經(jīng)生成的 Index 依然有效。

總結(jié): 在開發(fā)環(huán)境中啟用 ensureIndex ,并且創(chuàng)建 Collection,然后在生產(chǎn)環(huán)境中禁止 ensureIndex 。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,640評論 5 6
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 9,039評論 0 6
  • 兒子放暑假了,開學(xué)就要上九年級,就如他們班主任所說的那樣,己經(jīng)是預(yù)備班了,預(yù)備迎接中考。 許多家長在放假前...
    艾陽陽閱讀 191評論 0 1
  • 學(xué)號01白立平 2017年,“醫(yī)改”這個名詞,想必對每一位醫(yī)務(wù)人員都很具沖擊力。雖然早在2009年,我國就已經(jīng)啟動...
    白立平閱讀 214評論 2 3
  • 正常寫一段代碼 如果此時self.view 沒有“ _clearButton” 就會造成崩潰: 此時可以在可能發(fā)生...
    sunflower1518閱讀 1,608評論 0 1

友情鏈接更多精彩內(nèi)容