为集合(表)中的字段建立索引可以提高查询速度,但是却增加了数据插入的速度。 一般对不经常变动的集合(表)建立索引。
demo.js:
// ........
var animalSchema = new Schema({
name: String,
type: String,
tags: { type: [String], index: true } // 第一种方式。 字段级别
});
animalSchema.index({ name: 1, type: -1 }); // 第二种方式。 schema级别 1:表示正序
// ........