Node.js 未创建Mongoose复合索引(太长)
我在MongoDB中的一个模式上创建多个复合索引时遇到一些问题。在使用MongoLab时,我知道由于命名问题,有些索引太长时不会创建。因此,我怀疑这可能是为什么一些没有被创造出来的原因Node.js 未创建Mongoose复合索引(太长),node.js,mongodb,mongoose,mlab,Node.js,Mongodb,Mongoose,Mlab,我在MongoDB中的一个模式上创建多个复合索引时遇到一些问题。在使用MongoLab时,我知道由于命名问题,有些索引太长时不会创建。因此,我怀疑这可能是为什么一些没有被创造出来的原因 var Schema = new mongoose.Schema({ ... }); // Created Schema.index({ one: 1, three: 1 }); // Not Created Schema.index({ one: 1, two: 1,
var Schema = new mongoose.Schema({ ... });
// Created
Schema.index({
one: 1,
three: 1
});
// Not Created
Schema.index({
one: 1,
two: 1,
three: 1,
four: 1,
five: 1,
six: 1,
seven: 1,
eight: 1,
nine: 1,
ten: 1
});
默认索引名()有一个限制 您可以通过将添加选项对象传递到
Schema.index
Schema.index({
one: 1,
two: 1,
three: 1,
four: 1,
five: 1,
six: 1,
seven: 1,
eight: 1,
nine: 1,
ten: 1 },
{ name: 'my_index_name' }
);
当创建第二个索引时,它会在屏幕上打印一些东西吗?你怎么知道第二个索引没有创建?你签入mongo shell了吗?@somallg我还没有添加任何日志记录。我确实检查了mongo shell,但它没有创建,因此我知道。当我手动尝试创建该索引时,会得到以下结果:“MongoDB生成的默认索引名称太长。请指定自定义名称。”您的权利是,默认索引名称有一个限制(),您可以通过向Schema.index
Schema.index传递添加选项来指定自定义名称({1:1,2:1,3:1,4:1,5:1,6:1,7:1,8:1,9:1,10:1},{name:'my_index_name'});
谢谢,这正是我要找的for@somallg你能加上这个作为答案吗?然后我可以把它标记为已解决