Data API updateMany fail when collection validation contains additionalProperties set to false

TLDR: the data api updateMany always fails when the collection validation has additionalProperties set to true.

I have a collection, testcoll, that contain only two fields, _id and status. I have validation on this collection:

{
  $jsonSchema: {
    bsonType: 'object',
    required: [
      '_id',
      'status'
    ],
    properties: {
      _id: {
        bsonType: 'objectId'
      },
      status: {
        bsonType: 'string',
        'enum': [
          'pending',
          'complete'
        ]
      }
    },
    additionalProperties: false
  }
}

Validation Action is “Error” and Validation Level is “Strict”.

There are two documents in the collection:

{
  "_id": {
    "$oid": "64c7b73c2fa2ebadfb9592e5"
  },
  "status": "pending"
},
{
  "_id": {
    "$oid": "64c7b73c2fa2ebadfb9592e6"
  },
  "status": "pending"
}

When I use the following data api query to update all documents by changing the status:

  curl -s ".../endpoint/data/v1/action/updateMany" \
  -X POST \
  -H "apiKey: <MY KEY>" \
  -H "Content-Type: application/ejson" \
  -H "Accept: application/json" \
  -d '{
    "dataSource": "<MY CLUSTER>",
    "database": "<MY DATABASE>",
    "collection": "testcoll",
    "filter": {},
    "update": {
      "$set": {
        "status": "complete"
      }
    }
  }'

it always returns the error:

"Failed to update documents: FunctionError: (DocumentValidationFailure) Plan executor error during findAndModify :: caused by :: Document failed validation"

Two important points:

  • If I set additionalProperties to true the data api query will work.
  • If I run the same query in Mongo Shell it will work, even if additionalProperties is false.

I’d really like to use both additionalProperties and the data api in my solution (which is a bit more substantial than shown here) but there seems to be an incompatibility between them?
Does anyone know of a solution / workaround?

Thanks

1 Like