Recieve bad request error when sending data to API

I am sending data to our data API and keep getting a response 400 bad request. I have written a C# console app to send some bogus data in before writing the production version.

I am following the information found here (https://www.mongodb.com/docs/atlas/api/data-api-resources/#update-a-single-document) which suggests json needs to be sent in the format of the request body.

Here is a copy of the json I am sending as the content of the http request which should in theory trigger a new document (with field values changed).

{	
	"dataSource":"mysource",
	"database":"mydb",	
	"collection":"mycollection",
	"filter": {
		"accountSalesforceReference":"willnotfindthis"
	},	 
	"update": {
		"$set":	{
			"accountName":"Test",
			"version":"1.0",
			"address": {
					"line1":"10 Bingo Road",
					"town":"Bingotown",
					"county":"Bingoshire",
					"postcode":"B1N G0",
					"country":"Bingo"
			},
			"billingAddress": {
					   "line1":"10 Bingo Road",
					   "town":"Bingotown",
					   "county":"Bingoshire",
					   "postcode":"B1N G0",
					   "country":"Bingo"		
			}
		}
	},
	"upsert":true
}

However, I receive a 400 error. If I try this directly from Insomnia/Postman I get the same error with some extra detail:

“Failed to update document: Invalid or unspecified ‘dataSource’, ‘database’, or ‘collection’ field’”

Which makes little sense since these are the first three values of the JSON data. I am also setting the content-type header and apikey header as suggested in the documentation above.

What is wrong here?

Hi @David_Hirst :wave:

Can you double check your dataSource value? I believe it should be the name of the cluster.

For example, I have a cluster named Cluster0, when I use a non-existing cluster name for dataSource:

curl --location --request POST 'https://data.mongodb-api.com/app/<REDACTED>/endpoint/data/v1/action/updateOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <REDACTED>' \
--data-raw '{
    "collection":"updatecoll",
    "database":"db",
    "dataSource":"RandomClusterName",
    "filter": {
                "accountSalesforceReference":"willnotfindthis"
        },
        "update": {
                "$set": {
                        "accountName":"Test",
                        "version":"1.0",
                        "address": {
                                        "line1":"10 Bingo Road",
                                        "town":"Bingotown",
                                        "county":"Bingoshire",
                                        "postcode":"B1N G0",
                                        "country":"Bingo"
                        },
                        "billingAddress": {
                                           "line1":"10 Bingo Road",
                                           "town":"Bingotown",
                                           "county":"Bingoshire",
                                           "postcode":"B1N G0",
                                           "country":"Bingo"
                        }
                }
        },
        "upsert":true
}'

Output (same error you’ve receieved):

"Failed to update document: Invalid or unspecified 'dataSource', 'database', or 'collection' field'"

When using the correct dataSource value:

curl --location --request POST 'https://data.mongodb-api.com/app/<REDACTED>/endpoint/data/v1/action/updateOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <REDACTED>' \
--data-raw '{
    "collection":"updatecoll",
    "database":"db",
    "dataSource":"Cluster0",
    "filter": {
		"accountSalesforceReference":"willnotfindthis"
	},	 
	"update": {
		"$set":	{
			"accountName":"Test",
			"version":"1.0",
			"address": {
					"line1":"10 Bingo Road",
					"town":"Bingotown",
					"county":"Bingoshire",
					"postcode":"B1N G0",
					"country":"Bingo"
			},
			"billingAddress": {
					   "line1":"10 Bingo Road",
					   "town":"Bingotown",
					   "county":"Bingoshire",
					   "postcode":"B1N G0",
					   "country":"Bingo"		
			}
		}
	},
	"upsert":true
}'

Output:

{"matchedCount":0,"modifiedCount":0,"upsertedId":"646e92ac77e0dbb07408d38f"}%

Look forward to hearing from you.

Regards,
Jason

Thank you Jason, it was indeed an issue with the data source name. I was mistakenly using the projects name not the clusters name. All working now!

1 Like

Good to know that it was just a simple change required. Thanks for updating the post to confirm David :slight_smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.