2017-06-14 43 views
0

我正在通过一个使用猫鼬来填充MongoDB的例子。Mongoose:schema.index在几行

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 }); 
+0

[猫鼬中/ MongoDB的创建索引万事]的可能的复制(https://stackoverflow.com/questions/ 12573753 /创建multifield-indexes-in-mongoose-mongodb) –

+0

我知道,我想知道两种方式之间的区别 – pivu0

回答

1

区别是2,就像在2个索引中一样。

这将创建3个独立的指标:

eventSchema.index({ name: 1 }); 
eventSchema.index({ date: 1 }); 
eventSchema.index({ venue: 1 }); 

这将创建1 compound index

eventSchema.index({ name: 1, date: 1, venue: 1 }); 

与早期版本的MongoDB,复合指数分别为查询创建索引的唯一途径匹配多个字段。但是,从版本2.6开始,MongoDB可以为单个查询组合多个单字段索引,这减少了使用复合索引(需要仔细规划将执行哪些查询和排序)的需要。

更多信息: