Skip to content

MongoDB: draft findByID uses unprefixed collection defaultSort, causing Cosmos DB queries to fail #19184

Open

Description

@matthew-a-thomas

Server log below showing the issue on a draft-enabled "Sermons" collection, with defaultSort: '-date'.

The [mongo-find] message came from these parameters in mongooseAdapter:

    connectOptions: { monitorCommands: true },
    afterOpenConnection: (adapter) => {
      if (!adapter.connection) return

      adapter.connection.getClient().on('commandStarted', ({ commandName, command }) => {
        if (commandName !== 'find') return

        const collection = command.find
        if (collection !== '_sermons_versions' && collection !== 'sermons') return

        const sort = command.sort instanceof Map ? Object.fromEntries(command.sort) : command.sort
        console.info('[mongo-find]', JSON.stringify({ collection, sort }))
      })
    },

The URL was https://___.azurewebsites.net/api/sermons/6ae2719ea832f7ddefad794c?draft=true

2026-10-29T02:34:09.4175356Z [mongo-find] {"collection":"_sermons_versions","sort":{"date":-1}}
2026-10-29T02:34:09.4861637Z [02:34:09] �[31mERROR�[39m: �[36mError=2, Details='Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 5860d133-2b03-49f1-8ec4-b138be98a797; Reason: (Message: {"Errors":["The index path corresponding to the specified order-by item is excluded."]}
2026-10-29T02:34:09.4862324Z ActivityId: 5860d133-2b03-49f1-8ec4-b138be98a797, Request URI: /apps/51daff63-3cd3-4994-9cb6-9bad8b3fccfb/services/f2e08cd5-3c2a-4cad-b034-6095521eb7a7/partitions/d342aeb3-0473-4314-ab3b-5a1f6885b7e6/replicas/134374643423396575s/, RequestStats: Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, SDK: Windows/10.0.20348 cosmos-netstandard-sdk/3.18.0);�[39m
2026-10-29T02:34:09.4862374Z     err: {
2026-10-29T02:34:09.4862408Z       "type": "MongoServerError",
2026-10-29T02:34:09.4862495Z       "message": "Error=2, Details='Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 5860d133-2b03-49f1-8ec4-b138be98a797; Reason: (Message: {\"Errors\":[\"The index path corresponding to the specified order-by item is excluded.\"]}\r\nActivityId: 5860d133-2b03-49f1-8ec4-b138be98a797, Request URI: /apps/51daff63-3cd3-4994-9cb6-9bad8b3fccfb/services/f2e08cd5-3c2a-4cad-b034-6095521eb7a7/partitions/d342aeb3-0473-4314-ab3b-5a1f6885b7e6/replicas/134374643423396575s/, RequestStats: Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, SDK: Windows/10.0.20348 cosmos-netstandard-sdk/3.18.0);",
2026-10-29T02:34:09.4870538Z       "stack":
2026-10-29T02:34:09.4870709Z           MongoServerError: Error=2, Details='Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 5860d133-2b03-49f1-8ec4-b138be98a797; Reason: (Message: {"Errors":["The index path corresponding to the specified order-by item is excluded."]}
2026-10-29T02:34:09.4870826Z           ActivityId: 5860d133-2b03-49f1-8ec4-b138be98a797, Request URI: /apps/51daff63-3cd3-4994-9cb6-9bad8b3fccfb/services/f2e08cd5-3c2a-4cad-b034-6095521eb7a7/partitions/d342aeb3-0473-4314-ab3b-5a1f6885b7e6/replicas/134374643423396575s/, RequestStats: Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, SDK: Windows/10.0.20348 cosmos-netstandard-sdk/3.18.0);
2026-10-29T02:34:09.4870862Z               at Connection.sendCommand (/node_modules/mongodb/lib/cmap/connection.js:306:27)
2026-10-29T02:34:09.4870896Z               at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
2026-10-29T02:34:09.4870949Z               at async Connection.command (/node_modules/mongodb/lib/cmap/connection.js:334:26)
2026-10-29T02:34:09.4870981Z               at async Server.command (/node_modules/mongodb/lib/sdam/server.js:194:29)
2026-10-29T02:34:09.4871013Z               at async tryOperation (/node_modules/mongodb/lib/operations/execute_operation.js:213:32)
2026-10-29T02:34:09.4871044Z               at async executeOperation (/node_modules/mongodb/lib/operations/execute_operation.js:78:16)
2026-10-29T02:34:09.4871097Z               at async FindCursor._initialize (/node_modules/mongodb/lib/cursor/find_cursor.js:62:26)
2026-10-29T02:34:09.4871129Z               at async FindCursor.cursorInit (/node_modules/mongodb/lib/cursor/abstract_cursor.js:632:27)
2026-10-29T02:34:09.487116Z               at async FindCursor.fetchBatch (/node_modules/mongodb/lib/cursor/abstract_cursor.js:666:13)
2026-10-29T02:34:09.4871191Z               at async FindCursor.next (/node_modules/mongodb/lib/cursor/abstract_cursor.js:342:17)
2026-10-29T02:34:09.4871239Z       "errorLabelSet": {},
2026-10-29T02:34:09.4871271Z       "errorResponse": {
2026-10-29T02:34:09.4871299Z         "ok": 0,
2026-10-29T02:34:09.4871446Z         "errmsg": "Error=2, Details='Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 5860d133-2b03-49f1-8ec4-b138be98a797; Reason: (Message: {\"Errors\":[\"The index path corresponding to the specified order-by item is excluded.\"]}\r\nActivityId: 5860d133-2b03-49f1-8ec4-b138be98a797, Request URI: /apps/51daff63-3cd3-4994-9cb6-9bad8b3fccfb/services/f2e08cd5-3c2a-4cad-b034-6095521eb7a7/partitions/d342aeb3-0473-4314-ab3b-5a1f6885b7e6/replicas/134374643423396575s/, RequestStats: Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, SDK: Windows/10.0.20348 cosmos-netstandard-sdk/3.18.0);",
2026-10-29T02:34:09.4871533Z         "code": 2,
2026-10-29T02:34:09.4871563Z         "codeName": "BadValue"
2026-10-29T02:34:09.4871593Z       },
2026-10-29T02:34:09.4871622Z       "ok": 0,
2026-10-29T02:34:09.4874063Z       "code": 2,
2026-10-29T02:34:09.4874131Z       "codeName": "BadValue"
2026-10-29T02:34:09.4874163Z     }

I believe this line in your source is relevant:

https://github.com/payloadcms/payload/blame/ae600cf5a6a8aa29a7f88ec6141cb755bf6c0bac/packages/db-mongodb/src/queryDrafts.ts#L56

This works around the issue (URL with ?draft=true works):

import { getQueryDraftsSort } from 'payload'
    afterCreateConnection: (adapter) => {
      const queryDrafts = adapter.queryDrafts.bind(adapter)
      adapter.queryDrafts = (args) => {
        if (args.sort) {
          return queryDrafts(args)
        } else {
          return queryDrafts({
            ...args,
            sort: getQueryDraftsSort({
              collectionConfig: adapter.payload.collections[args.collection].config,
            }),
          })
        }
      }
    }

I found this comment from Aug 2024:

#7187 (comment)

looks like defaultSort does not work with the collection list page and versions, it attempts to sort on a root field for the _versions mongo table which instead lives under the version field

Activity

  1. matthew-a-thomas commented on Oct 29, 2026

    @matthew-a-thomas
    Author

    Payload and @payloadcms/db-mongodb 3.91.0

    Cosmos DB for MongoDB (RU), with indexSortableFields: true and disableFallbackSort: true

    _sermons_versions already has an index on version.date, while the captured query sorts on date

    Likely repro steps:

    1. Configure a collection with drafts enabled, a date field named date, and defaultSort: '-date'
    2. Create and publish a document
    3. Request /api/sermons/
      • succeeds
    4. Request /api/sermons/?draft=true
      • returns HTTP 500
    5. Command monitoring shows _sermons_versions being sorted by { date: -1 }, although the field is stored and indexed as version.date
    6. Supplying the translated default through the workaround above makes the same request succeed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions