Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do 1 and -1 stand for in mongoose compound indexes?

You can see it in the example in their official docs: 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

Why is name set to 1 and type set to -1?

like image 679
Louay Alakkad Avatar asked Mar 15 '23 02:03

Louay Alakkad


1 Answers

From the mongodb docs

Sort Order

Indexes store references to fields in either ascending (1) or descending (-1) sort order.

See here: https://docs.mongodb.org/manual/core/index-compound/

So, as per your example, name is ascending, type is desecending

like image 75
Alex Avatar answered Mar 24 '23 03:03

Alex