# MongoDB Atlas Data API - 再次解放你的雙手 我之前介紹過一個 MongoDB 的託管服務 [MongoDB Atlas](https://www.mongodb.com/cloud/atlas/register) ![](https://i.imgur.com/inEOtOK.png) 讓我們有個免費的方案可以使用 MongoDB Cluster 進行一些 Side Project ## 建完資料庫就是完成了 CRUD API [一個 PostgreSQL 搞定 CRUD - PostgREST](https://hackmd.io/0KjN3fvFSUu1p5h32mjWQQ) 我在裡面分享過的,當我們把資料庫建立起來的同時,也有了 REST API 可以對其進行一些基本的 CRUD 操作,而不需要在專案中依賴 DB Driver 就可以簡單地進行溝通了。 那,MongoDB 是不是也可以做到類似的操作? ## MongoDB Atlas Data API >The MongoDB Atlas Data API lets you read and write data in Atlas with standard HTTPS requests. To use the Data API, all you need is an HTTPS client and a valid API key. ![](https://i.imgur.com/4kpaUyj.png) 我們全部需要的東西只有兩個! 1. HTTPS client 2. API KEY ```js curl --location --request POST 'https://data.mongodb-api.com/app/data-biaye/endpoint/data/v1/action/findOne' \ --header 'Content-Type: application/json' \ --header 'Access-Control-Request-Headers: *' \ --header 'api-key: <API_KEY>' \ --data-raw '{ "collection":"<COLLECTION_NAME>", "database":"<DATABASE_NAME>", "dataSource":"<CLUSTER_NAME>", "projection": {"_id": 1} }' ``` 只需要去生產出屬於你的 API Key 帶入上方的 request header,填寫你需要操作的 datasource, database, collection,而 path variable 的最後一個則是指令,像是例子使用的是 `findOne` 下圖為實際上操作取得的資料,基本上我們只需要置換後方的指令為 insertOne(),那也就可以新增一筆資料進去了。 ![](https://i.imgur.com/tIMLRiW.png) ## 注意 ### Monthly Free Tier > All App Services Apps in a MongoDB Atlas project share a single monthly free tier. All usage below the free tier thresholds in a given month is not billed. As soon as a project exceeds any monthly free tier threshold, App Services starts billing for additional usage of any kind for that project. #### **Free tier thresholds** App Services is free to use below the following monthly free tier thresholds: - 1,000,000 requests or 500 hours of compute or 10,000 hours of sync runtime (whichever occurs first) - 10GB of data transfer ### Request Limitations - The Data API enforces the following limitations on all requests: - Incoming requests may not exceed 18 MB. - Response body content may not exceed 16 MB. - Request processing time may not exceed 90 seconds. ## 連結 https://www.mongodb.com/docs/atlas/api/data-api/ https://www.mongodb.com/docs/atlas/app-services/billing/