2 / 2
Sep 2024

Hello,

I’m having an issue using atlas app services data api. I am attempting to insert one document as per the call below:

curl --location 'https://eastus2.azure.data.mongodb-api.com/app/APP_ID/endpoint/data/v1/action/insertOne' \ --header 'apiKey: API_KEY' \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --data '{ "dataSource": "mongodb-atlas", "database": "DATABASE_NAME", "collection": "COLLECTION_NAME", "document": { "name": "Test", "assetId": 1234 } }'

My app services schema for property is defined as:

{ "title": "property", "properties": { "_id": { "bsonType": "objectId" }, "name": { "bsonType": "string" }, "assetId": { "bsonType": "int" } }, "required": [ "_id", "name", "assetId" ] }

The call fails with the following error (from the app services logs):

ERROR: could not validate document '' for insert { "name": "data/v1/insertOne" }

However, if I provide an _id objectId in the payload, the insert succeeds - I was under the impression that I would never have to provide that id, that it would be generated by the insert.

I appreciate any help with this.

28 days later

Easiest way to fix this, as it works on my test cluster.

If you want MongoDB to automatically generate the _id field, you can modify your schema to make the _id field optional. Remove _id from the required array in your schema.

Although I am going to warn you, that removing stuff from your schema is a destructive change that can have impact down the road. But I’m not sure if going from required to option would make it destructive though.