Lakhwinder Singh
Lakhwinder Singh

Reputation: 5582

How to Improve Performance of MongoDB Aggregation Pipeline with Atlas Search and Grouping?

I'm using MongoDB Atlas Search index across multiple fields in my collection. I've created an aggregation pipeline with two stages: the first stage performs a keyword search using Atlas Search, and the second stage groups the field values along with their counts. However, I'm experiencing significant performance issues where the second stage takes approximately 50 to 60 seconds to load data.

Below is the aggregation pipeline I'm using:

[
  {
    $search: {
      index: "keyword_search_index",
      queryString: {
        defaultPath: "title",
        query: '((title: ("product") OR description: ("product") OR subdocuments.name: ("product") OR company.name: ("product") OR skills.name: ("product") OR title: ("products") OR description: ("products") OR subdocuments.name: ("products") OR company.name: ("products") OR skills.name: ("products")))'
      }
    }
  },
  {
    $unwind: {
      path: "$subdocuments"
    }
  },
  {
    $group: {
      _id: {
        code: "$subdocuments.code",
        name: "$subdocuments.name"
      },
      count: {
        $sum: 1
      }
    }
  }
]

How can I improve the performance of this aggregation pipeline? Any suggestions or best practices to optimize both the search and grouping stages are highly appreciated.

Upvotes: 0

Views: 184

Answers (0)

Related Questions