gpt4 book ai didi

javascript - 我可以在模式路径级别和模式级别建立 Mongoose 索引吗?

转载 作者:行者123 更新时间:2023-12-02 23:39:28 24 4
gpt4 key购买 nike

根据 Mongoose 文档,关于 https://mongoosejs.com/docs/guide.html#indexes :

“使用 mongoose,我们在架构中的路径级别或架构级别定义这些索引。”

澄清一下:这意味着我不能有两个不同的索引,一个在架构路径级别定义,另一个在架构级别定义?例如:

var animalSchema = new Schema({
name: String,
age: String,
tags: { type: [String], index: true } // field level
});

animalSchema.index({ name: 1, type: -1 }); // schema level

最佳答案

字段级别的只是 .index 函数的糖语法。

因此,如果您已经使用 .index 函数在字段上定义了索引,则它不会再执行任何操作。

在您的示例中,将创建 tags 上的索引以及 name + type 上的索引。

这个:

var animalSchema = new Schema({
name: String,
age: String,
tags: { type: [String], index: true } // field level
});

相当于这个:

var animalSchema = new Schema({
name: String,
age: String,
tags: { type: [String] }
});

animalSchema.index({ tags: 1 }); // schema level

关于javascript - 我可以在模式路径级别和模式级别建立 Mongoose 索引吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56140593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com