【问题标题】:Indexing with Mongoose使用 Mongoose 进行索引
【发布时间】:2021-01-23 07:50:49
【问题描述】:

我正在阅读有关索引的 mongoose 文档,并想了解字段级索引和架构级索引之间是否存在差异。他们提到“在创建复合索引时需要在模式级别定义索引”。是否有任何其他原因可以让我选择一个而不是另一个,或者这只是一种偏好?

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

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

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema mongodb-indexes


    【解决方案1】:

    在制定索引策略时,您应该深入了解应用程序的查询。在构建索引之前,请绘制出您将运行的 queries 的类型,以便您可以构建引用这些字段的索引。 例如,您有一个基于 name 之类的字段的查询,因此您可以在 field level 中索引 name 但如果您有一个基于 nametag 查找的查询,那么您应该索引 @987654327 @ 和 tag 一起在这种情况下,您应该在 schema level 中使用索引

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 2015-02-10
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 2016-03-30
      • 2019-05-27
      • 2017-04-09
      相关资源
      最近更新 更多