Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

node.js - Indexing with Mongoose

I was reading the mongoose docs about indexing and want to find out whether there is a difference between field level indexing and schema level indexing. They mention that "defining indexes at the schema level is necessary when creating compound indexes." Are there any other reasons why I might choose one over the other or it is just a preference?

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

  animalSchema.index({ name: 1, type: -1 }); // schema level
question from:https://stackoverflow.com/questions/65856958/indexing-with-mongoose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When developing your indexing strategy you should have a deep understanding of your application’s queries. Before you build indexes, map out the types of queries you will run so that you can build indexes that reference those fields. for example you have a query just find based on one field like name so you can indexing name in field level but if you have a query that find based on name and tag so you should indexing name and tag together for this situation you should use indexing in schema level


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...