Node.js 嵌套文档中字段的Mongoose索引
我有一个小模式Node.js 嵌套文档中字段的Mongoose索引,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个小模式 var PostSchema = new mongoose.Schema({ title: String, link: String, author: {type:String,required:true}, upvotes: {type: Number, default: 0}, nesteddoc : { field1: String } }); //This is broken - index on field1 PostSchema.
var PostSchema = new mongoose.Schema({
title: String,
link: String,
author: {type:String,required:true},
upvotes: {type: Number, default: 0},
nesteddoc : {
field1: String
}
});
//This is broken - index on field1
PostSchema.index({nesteddoc.field1:1},{unique:true});
是否可以通过在Mongoose模式中指定而不运行MongoDB查询来确保索引,从而在嵌套字段上建立索引?使用“nesteddoc.field1”周围的引号来计算嵌套字段:
PostSchema.index({ "nesteddoc.field1": 1 }, { unique: true });
此外,mongoose将从以下位置在内部调用ensureIndex
:
当应用程序启动时,Mongoose会自动调用
确保模式中每个已定义索引的索引。猫鼬会打电话来
确保按顺序为每个索引创建索引,并在
当所有ensureIndex调用成功或
一个错误。虽然对于开发来说很好,但建议使用这种行为
在生产中被禁用,因为索引创建可能会导致
性能影响。通过设置自动索引禁用该行为
将您的模式选项设置为false,或通过连接全局设置为false
将选项config.autoIndex设置为false
您还可以在架构中定义索引:
var PostSchema = new mongoose.Schema({
title: String,
link: String,
author: { type: String, required: true },
upvotes: { type: Number, default: 0 },
nesteddoc: {
field1: { type: String, unique: true, index: true },
}
});
如何为全文搜索索引相同的嵌入字段?PostSchema.index({“nesteddoc.field1”:“text”});是否有人可以提供一个指向mongoose文档页面的链接,其中说明了这一点?索引中的1是什么?为什么没人解释呢?@shinzou1是升序指数,-1是降序指数,参见