问题
I'm using MongoDB Stitch to create a data enabled API, but when I make a GET request, the data is returned where numbers are displayed as:
"firstHit": {
"$numberInt": "3"
Where I would like them to be return just as:
"firstHit": 3
I have a lot of objects within objects, and I am inserting the data through the mongo shell, I'm not sure of that is of any importance.
Anyone have any experience with this? Thank you!
回答1:
By default, the result format returned by MongoDB Stitch webhooks is in MongoDB Extended JSON format, or EJSON
for short. This is useful to define data types that would otherwise be lost in normal JSON. There are some object types that have no equivalent in JSON, for example ObjectId() and Date().
If you would like to return as a normal JSON, you could set the response object as an example below:
exports = function(payload, response) {
result = {"firsthit": 10};
response.setStatusCode(200);
response.setHeader("Content-Type", "application/json");
response.setBody(JSON.stringify(result));
}
You may also find EJSON library and Stitch Utility Packages as useful additional information.
来源:https://stackoverflow.com/questions/61922685/mongodb-stitch-returns-data-as-numberdouble-instead-of-the-number-itself