mongoose中給字段添加索引的方法有兩種,一種通過在定義schema的時候配置,如:
1 var animalSchema = new Schema({ 2 name: String, 3 type: String, 4 tags: { type: [String], index: true }
另一種通過index方法添加索引,如給name和type字段添加索引(1和-1分別表示升序索引和降序索引):
animalSchema.index({ name: 1, type: -1 });