简体   繁体   English

MongoDB Stitch 将数据返回为 $NumberDouble 而不是数字本身

[英]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:我正在使用 MongoDB Stitch 创建启用数据的 API,但是当我发出 GET 请求时,将返回数据,其中数字显示为:

"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.我在对象中有很多对象,我正在通过 mongo shell 插入数据,我不确定这是否重要。

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.默认情况下, MongoDB Stitch webhooks 返回的结果格式为MongoDB Extended JSON格式,简称EJSON This is useful to define data types that would otherwise be lost in normal JSON.这对于定义在正常 JSON 中会丢失的数据类型很有用。 There are some object types that have no equivalent in JSON, for example ObjectId() and Date() .有一些 object 类型在 JSON 中没有等价物,例如ObjectId()Date()

If you would like to return as a normal JSON, you could set the response object as an example below:如果您想以正常 JSON 的形式返回,您可以将响应 object设置为以下示例:

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.您还可以找到EJSON 库Stitch Utility Packages作为有用的附加信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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