Search code examples
azureazure-cosmosdbazure-cosmosdb-mongoapi

How to handle Azure CosmosDB & MongoDB Error 2 BadRequest


I am getting the following errors on my Azure CosmosDB deployment of MongoDB:

MongoError: Error=2, Details='Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: b83905b9-d5d4-41f2-adfb-80b760e04327; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: b83905b9-d5d4-41f2-adfb-80b760e04327; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: b83905b9-d5d4-41f2-adfb-80b760e04327; Reason: (Message: {"Errors":["Encountered exception while executing function. Exception = Error: Invalid argument. Only JavaScript regex flags are supported (i, g, m, u)\r\nStack trace: Error: Invalid argument. Only JavaScript regex flags are supported (i, g, m, u)\n   at regex_match (script.js:65:21)"]}
ActivityId: b83905b9-d5d4-41f2-adfb-80b760e04327, Request URI: /apps/787694ff-190a-4607-aa8d-54f0f1a41975/services/3ccd1c8a-f040-412f-8758-65417090d613/partitions/e1ee63ae-b6e1-4a16-b4e9-8cfb2054067a/replicas/132587547149035411s/, RequestStats: Please see CosmosDiagnostics, SDK: Windows/10.0.17763 cosmos-netstandard-sdk/3.3.2);););
    at MessageStream.messageHandler (/var/www/SCWebMarketplace/server/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (/var/www/SCWebMarketplace/server/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/var/www/SCWebMarketplace/server/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at TLSSocket.ondata (internal/streams/readable.js:719:22)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:223:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
    at TLSWrap.callbackTrampoline (internal/async_hooks.js:131:14) {

This does not occur on any local dev machines, only on the CosmosDB.

The code being run in this call is as follows:

    const foundUsers = await User.find({
      $and: [
        { isCreator: true },
        {
          $or: [
            {
              username: { $regex: text, $options: 'ix' },
            },
            {
              firstName: { $regex: text, $options: 'ix' },
            },
            {
              lastName: { $regex: text, $options: 'ix' },
            },
          ],
        },
      ],
    }).populate('createdStories');

Solution

  • The solution was removing the "x" characters from the regex query.

        const foundUsers = await User.find({
          $and: [
            { isCreator: true },
            {
              $or: [
                {
                  username: { $regex: text, $options: 'i' },
                },
                {
                  firstName: { $regex: text, $options: 'i' },
                },
                {
                  lastName: { $regex: text, $options: 'i' },
                },
              ],
            },
          ],
        }).populate('createdStories');
    

    I am still not quite sure why CosmosDB has this restriction, but the relevant part of the error message was this:

    Only JavaScript regex flags are supported (i, g, m, u)