猫鼬复合索引中的 1 和 -1 代表什么?

Lou*_*kad 4 mongoose mongodb mongoose-schema

您可以在他们的官方文档中的示例中看到它:guide#indexes

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

animalSchema.index({ name: 1, type: -1 }); // schema level
Run Code Online (Sandbox Code Playgroud)

为什么名称设置为1和类型设置为-1

Ale*_*lex 7

来自 mongodb 文档

排序

索引以升序 (1) 或降序 (-1) 排序顺序存储对字段的引用。

请参阅此处:https : //docs.mongodb.org/manual/core/index-compound/

所以,根据你的例子,name是上升的,type是下降的