Answer the question
In order to leave comments, you need to log in
How to optimize mongodb query?
Hello everyone, can you tell me how to optimize the query below, or maybe even revise the structure of the document. Any information would be helpful.
There are documents of the form, I list only the most important fields
{
url: 'string',
public: 'number',
tags: 'Array(String)',
catalog: 'ObjectId()'
}
{
url: 'url-1',
public: 1,
tags: [ 'чай', 'кофе' ], // В большинстве своем содержит от 5 до 12 значений
catalog: ObjectId('db.catalogs._id')
}
return Collection
.aggregate([
{
$match: {
url: { $ne: request.params.document },
public: { $gte: 2 },
tags: { $in: request.items.tagsSlice }, // request.items.tagsSlice - первые 5 тегов основной записи
catalog: request.items.catalog._id
}
},
{ $sort: { tags: 1 } },
{ $skip: 0 },
{ $limit: 12 },
{
$project: {
/****/
}
}
])
.then()
schema.index( { url: 1, public: 1, tags: 1, catalog: 1, tags: 1 } )
Answer the question
In order to leave comments, you need to log in
Make custom index for tags
schema.index( { url: 1 } )
schema.index( { tags: 1 }, {sparse: true} )
schema.index( { catalog: 1 } )
{ $sort: { catalog: 1, tags: 1 } },
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question