Mongodb: when to call ensureIndex?

后端 未结 7 1030
囚心锁ツ
囚心锁ツ 2021-02-01 01:34

When should I call ensureIndex? Before inserting a single record, after inserting a single record, or before calling find()?

Regards,

Johnny

7条回答
  •  无人共我
    2021-02-01 02:06

    You only need to do this once. Example:

    db.table.insert({foo: 'bar'});
    var foo = db.table.findOne({foo: 'bar'}); // => delivered from FS, not RAM
    db.table.ensureIndex({foo: 1});
    var foo = db.table.findOne({foo: 'bar'}); // => delivered from RAM, not FS
    db.table.insert({foo: 'foo'});
    var foo = db.table.findOne({foo: 'foo'}); // => delivered from RAM, not FS
    

提交回复
热议问题