API with a single endpoint that fetches the data from the MongoDB collection and return the results in the requested format
- nodejs version >= 14
- npm version >= 7
- mongoDB
npm i
- install dependencies- set environment variables
PORT
andMONGODB_CONNECTION_URL
npm start
- run the server
To run the tests:
npm test
After server is started you can send a POST request it.
The request payload will include a JSON with 4 fields:
startDate
andendDate
fields will contain the date in aYYYY-MM-DD
format. They are used to filter the data usingcreatedAt
property.minCount
andmaxCount
are for filtering the data. Sum of thecount
array in the documents should be betweenminCount
andmaxCount
.
Sample:
{
"startDate": "2016-01-26",
"endDate": "2018-02-02",
"minCount": 2700,
"maxCount": 3000
}
Response payload has 3 main fields:
code
is for status of the request. 0 means success. See more details about error codes belowmsg
is for description of the code. It contains "success" for successful requests. For unsuccessful requests it provides explanatory messages.records
will include all the filtered items according to the request. This array should include items ofkey
,createdAt
andtotalCount
which is the sum of thecounts
array in the document.
Sample:
{
"code": 0,
"msg": "success",
"records": [
{
"key":"TAKwGc6Jr4i8Z487",
"createdAt":"2017-01-28T01:22:14.398Z",
"totalCount":2800
}, {
"key":"NAeQ8eX7e5TEg7oH",
"createdAt":"2017-01-27T08:19:14.135Z",
"totalCount":2900
}
]
}
{"code": 1, "msg": "system error"}
- general system error, all unexpected error will have this response{"code": 2, "msg": "request.body.minCount should be number"}
- request payload validation error,msg
will contain the details. Check you send request payload using expected format{"code": 3, "msg": "startDate should be less than endDate"}
- error withstartDate
/endDate
{"code": 4, "msg": "minCount should be less that maxCount"}
- error withminCount
/maxCount