我正在研究一个使用猫鼬来填充MongoDB的示例。
var eventSchema = new mongoose.Schema({
_id: { type: String },
name: { type: String, required: true},
description: {type: String},
venue: {type: String}, });
eventSchema.plugin(require('./plugins/pagedFind'));
eventSchema.index({ name: 1 });
eventSchema.index({ date: 1 });
eventSchema.index({ venue: 1 });我不知道schema.index是什么;所以我查找了它;http://mongoosejs.com/docs/guide.html;我学到的是,除了默认的_id排序之外,这是对集合进行排序的一种方法。
但是我写的和下面的有什么区别吗?
eventSchema.index({ name: 1, date: 1,venue: 1 });发布于 2017-06-14 17:35:19
差别是2,就像在2个索引中一样。
这将创建3个单独的索引:
eventSchema.index({ name: 1 });
eventSchema.index({ date: 1 });
eventSchema.index({ venue: 1 });这就创建了一个索引
eventSchema.index({ name: 1, date: 1, venue: 1 });对于早期版本的MongoDB,复合索引是为匹配多个字段的查询创建索引的唯一方法。
但是,从版本2.6开始,MongoDB可以为单个查询组合多个单字段索引,这减少了使用复合索引的需要(这需要对执行哪些查询和排序进行一些仔细的规划)。
更多信息:
https://stackoverflow.com/questions/44550685
复制相似问题