MongoDB ArrrayFilters taking too much time to update

I am using arrayFilters to update an element within a document, but its taking more than 200 ms. I am not able to identify the bottleneck. We are expecting a response time of ~30ms.
Document size is 125kB and total nbr of documents in MongoDB- 25k

Snippet of the document -

{
"_id": "FlightLegPaxRecord::UA::1494::13-AUG-2020::SFO::EWR",
"travelerCounts": [
  {
    "counts": [
      {
        "key": "J",
        "value": "16"
      },
      {
        "key": "Y",
        "value": "153"
      }
    ],
    "travelCountType": "ActualCapacity"
  },
  {
    "counts": [
      {
        "key": "J",
        "value": "16"
      },
      {
        "key": "Y",
        "value": "153"
      }
    ],
    "travelCountType": "AdjustedCapacity"
  },

If I go for a compound index will it help - My understanding - the document is first retrieved by _id and then arrayFilters will be applied to get the correct position in the array. Please confirm if my understanding is correct and would it help adding the below compound index ? what can be done to improve the performance of the update query

{"travelerCounts.travelCountType": 1, "travelerCounts.counts.key": 1}

Snippet of java code -

MongoCollection collection = database.getCollection(colName);

Map<String, String> arrayFilters = new HashMap<String, String>();
					arrayFilters.put("travelerCounts.travelCountType", "AdjustedCapacity");
					arrayFilters.put("counts.key", "Y");
UpdateOptions updateOptions = new UpdateOptions().arrayFilters(arrayFiltersAdd);

UpdateResult result = collection.updateOne(Filters.eq("_id", id), Updates.set("travelerCounts.$[travelerCounts].counts.$[counts].value", "154"), updateOptions);

@samaresh_kirtania I’d recommend you put the commands / queries rather than the java code, as not everybody will understand it.
I did get an idea, but as it doesnt have the full query, I cannot answer for sure.

1 Like