'When using MongoDB with Mongoose, what method and syntax can you use to search compound indexes?

I'm just trying out searching indexes today for the first time and i'm running into trouble with finding out how to search a database using compound indexes. My compound index has the following data inside in order.

product_id: Number

question_id: Number

reported: Boolean

I left the code for initializing the database. Here's how I tried to query the database using hardcoded data. I'm not sure if i'm supposed to use the .find method.

res is an express server response to send data back to the client.

The .find method below runs but it runs a colscan rather than searching through the indexes. What is the proper syntax to search through the newly created indexes?

   const mongoose = require('mongoose');

const answersSchema = new mongoose.Schema({
  id: Number,
  product_id: Number,
  body: String,
  date_written: Number,
  asker_name: String,
  asker_email: String,
  reported: Boolean,
  helpful: Number,
});

    Answers = mongoose.model("answers", answersSchema);


  Answers.find({product_id: 6879306, id: 3518963, reported: false}, (err, answer) => {
    if (err) {
      console.log('failed in controllers getQuestions.js');

    } else {
      console.log('the goods', answer);
      res.send(answer);
    }
  });
}


Solution 1:[1]

So with some help, I've gotten to the bottom of my problem. I was doing a column search because I didn't set the schema to set them as indexes too. Here's the new schema

const answersSchema = new mongoose.Schema({
  product_id: {
    type: Number,
    index: true
  },
  id: {
    type: Number,
  },
  reported: {
    type: Boolean,
    index: true,
  },
  body: String,
  date_written: Number,
  asker_name: String,
  asker_email: String,
  helpful: Number,
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HigherOrderChilli