im building a mongodb app and im just wondering why (even with indexes) my requests getting very slow if documents reach an amount > 1.000.000.
my schema looks like:
const reportSchema = new mongo.Schema({
organisation: {
type: mongo.Schema.Types.ObjectId,
ref: 'Organisation',
required: true
},
reference: {
type: mongo.Schema.Types.ObjectId,
required: true
},
belongsTo: {
type: String,
enum: ['endpoint'],
required: true
},
objects: [{
property: {
type: String,
required: true
},
value: {
type: String,
required: true
}
}]
}, {
timestamps: true
});
and my index looks like:
reportSchema.index({
createdAt: -1,
organisation: 1,
reference: 1
});
my query:
this.model.find({
organisation: organisationId,
reference: referenceId
})
.select('_id reference objects createdAt')
.sort('-createdAt')
.then(reports => resolve(reports))
.catch(err => {
console.error(err);
reject();
});
can anybody tell me why? is the index the point of failure? is my query wrong?