MongoDB huge amount of temp files

I have a MongoDB replica set. The primary one creates a huge amount of temp files with the name extsort-sort-executor in every seconds, which generates more than 80Gb of temp files under 10 minutes…

If I set the allowDiskUseByDefault to false I get the following error:
Plan executor error during find command

  "stats": {
    "works": 88450,
    "restoreState": 88,
    "saveState": 88,
    "advanced": 0,
    "spilledDataStorageSize": 0,
    "nReturned": 0,
    "inputStage": {
      "works": 88450,
      "restoreState": 88,
      "stage": "COLLSCAN",
      "saveState": 88,
      "advanced": 88450,
      "nReturned": 88450,
      "needYield": 0,
      "docsExamined": 88450,
      "needTime": 0,
      "isEOF": 0,
      "direction": "forward"
    },
    "failed": true,
    "usedDisk": false,
    "type": "simple",
    "isEOF": 0,
    "memLimit": 104857600,
    "stage": "SORT",
    "sortPattern": {
      "-$natural": 1
    },
    "needYield": 0,
    "needTime": 88449,
    "totalDataSizeSorted": 0,
    "spills": 0
  },
  "cmd": {
    "filter": {},
    "lsid": {
      "id": {
        "$uuid": "2617b402-0c61-4035-bc39-d616e412fbb3"
      }
    },
    "$readPreference": {
      "mode": "primary"
    },
    "$db": "local",
    "$clusterTime": {
      "clusterTime": {
        "$timestamp": {
          "t": 1704123123,
          "i": 1
        }
      },
      "signature": {
        "keyId": 0,
        "hash": {
          "$binary": {
            "base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=",
            "subType": "0"
          }
        }
      }
    },
    "find": "oplog.rs",
    "sort": {
      "-$natural": 1
    }
  },
  "error": {
    "code": 292,
    "codeName": "QueryExceededMemoryLimitNoDiskUseAllowed",
    "errmsg": "Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting."
  }
}

Can someone point me to the right direction where can I found the source of issue?

Is this a query that you are running? If so, to use a natural sort order the correct format is {"$natural": -1}

Using the correct format will avoid an in memory sort (and disk usage when enabled).

I have found the related issue. Metricbeat MongoDB module does the query:

Unfortunatelly, still no update for this issue…

Hi @SnackK,

That lead me to a couple of elastic JIRA you can comment/keep an eye on. Definitely a bug in the beat.

https://github.com/elastic/beats/issues/33616
https://github.com/elastic/beats/issues/36110

Issue from the 8.11.3 tagged source:

Incorrect sort field:
https://github.com/elastic/beats/blob/8c4a40fdbfda1a8e5ff2ceae01930a9f3e629d97/metricbeat/module/mongodb/replstatus/info.go#L79
and
Value: 1 will not give the reverse order needed for lastTs:
https://github.com/elastic/beats/blob/8c4a40fdbfda1a8e5ff2ceae01930a9f3e629d97/metricbeat/module/mongodb/replstatus/info.go#L96