MongoDB Stitch 将数据返回为 $NumberDouble 而不是数字本身 [英] MongoDB Stitch returns data as $NumberDouble instead of the number itself

查看:43
本文介绍了MongoDB Stitch 将数据返回为 $NumberDouble 而不是数字本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MongoDB Stitch 创建一个启用数据的 API,但是当我发出 GET 请求时,数据被返回,其中的数字显示为:

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

我在对象中有很多对象,我正在通过 mongo shell 插入数据,我不确定这是否重要.

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!

推荐答案

默认情况下,返回的结果格式MongoDB Stitch 网络钩子采用 MongoDB 扩展 JSON 格式,或简称 EJSON.这对于定义在普通 JSON 中会丢失的数据类型很有用.有些对象类型在 JSON 中没有等价物,例如 ObjectId()Date().

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().

如果您想作为普通 JSON 返回,您可以设置 响应对象如下例:

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));
}

您还可以找到 EJSON 库Stitch Utility Packages 作为有用的附加信息.

You may also find EJSON library and Stitch Utility Packages as useful additional information.

这篇关于MongoDB Stitch 将数据返回为 $NumberDouble 而不是数字本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆