We have a fairly complicated aggregation query used for gluing some data together and providing it in a server-side paging application. What is the best way to determine what the indexes should be for this kind of complex query? I thought that Mongodb would “recommend indexes” like our SQL Server does. I have seen that in the past, but we have not received any guidance for this collection. It’s fairly new - is there some way to accelerate the recommendations?
Query below as an example, but I don’t expect you to be able to understand it without some explanation of our crazy data
[
{
$graphLookup:
{
from: "FlatSite",
startWith: "$ParentRowId",
connectFromField: "ParentRowId",
connectToField: "RowId",
maxDepth: 10,
as: "Parents",
depthField: "level",
},
},
{
$match:
{
LevelId: 2,
},
},
{
$project:
{
_id: 1,
Columns: 1,
Value: 1,
RowId: 1,
ParentColumns: {
$reduce: {
input: "$Parents.Columns",
initialValue: [],
in: {
$concatArrays: [
"$$value",
"$$this",
],
},
},
},
},
},
{
$project:
{
_id: 1,
Value: 1,
RowId: 1,
Columns: {
$concatArrays: [
"$Columns",
"$ParentColumns",
],
},
},
},
{
$project:
{
_id: 1,
Value: 1,
RowId: 1,
Columns: {
$filter: {
input: "$Columns",
as: "column",
cond: {
$in: [
"$$column.ColumnId",
[
ObjectId(
"60707b306d3a5d6157bfe469"
),
ObjectId(
"60707b336d3a5d6157bfe47c"
),
ObjectId(
"64ad9acfff44fd298888d34e"
),
],
],
},
},
},
},
},
{
$match:
{
Columns: {
$all: [
{
$elemMatch: {
ColumnId: ObjectId(
"60707b306d3a5d6157bfe469"
),
$and: [
{
Value: /^E-211$/i,
},
],
},
},
],
},
},
},
{
$project:
{
_id: 1,
Columns: 1,
Value: 1,
RowId: 1,
SortColumn1: {
$arrayElemAt: [
{
$filter: {
input: "$Columns",
as: "c",
cond: {
$eq: [
"$$c.ColumnId",
ObjectId(
"60707b306d3a5d6157bfe469"
),
],
},
},
},
0,
],
},
SortColumn2: {
$arrayElemAt: [
{
$filter: {
input: "$Columns",
as: "c",
cond: {
$eq: [
"$$c.ColumnId",
ObjectId(
"64ad9acfff44fd298888d34e"
),
],
},
},
},
0,
],
},
},
},
{
$setWindowFields:
{
output: {
TotalRecords: {
$count: {},
},
},
},
},
{
$sort:
/**
* Provide any number of field/order pairs.
*/
{
"SortColumn1.Value": -1,
"SortColumn2.Value": -1,
},
},
{
$skip:
/**
* Provide the number of documents to skip.
*/
0,
},
{
$limit:
/**
* Provide the number of documents to limit.
*/
100,
},
{
$project:
/**
* specifications: The fields to
* include or exclude.
*/
{
_id: 0,
SortColumn1: 0,
SortColumn2: 0,
},
},
]