简体   繁体   中英

MongoDB Stitch returns data as $NumberDouble instead of the number itself

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM