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);
}
});
}