我已经创建了一个简单的领域应用程序,它将使用HTTPS端点来运行运行聚合管道的函数。
这个函数看起来是这样的。
exports = async function (request, response) {
const pipeline = [
{
'$project': {
'_id': 0
}
}, {
'$group': {
'_id': '$vaultId',
'vaultName': {
'$first': '$vaultName'
},
'vaultContract': {
'$first': '$vaultContract'
},
'prices': {
'$push': {
'readingDate': '$readingDate',
'spotPrice': '$spotPrice'
}
}
}
}, {
'$sort': {
'_id': 1,
'readingDate': -1
}
}
];
requestResponse = await context.services
.get("mongodb-atlas")
.db("mydb")
.collection("mycollection")
.aggregate(pipeline);
return requestResponse;
};在设置HTTPS端点时,我可以选择返回类型为JSON或EJSON。当选择EJSON时,它工作得很好,但是在响应中包含了一些不需要的附加细节。当切换到JSON时,响应会失败。
现在的反应是这样的..。
...
,
{
"vaultId": {
"$numberInt": "552"
},
"vaultName": "RR/BAYC",
"vaultContract": "0xcd2e3a66507e94190e3b1521a189ad821c8c3006",
"prices": [
{
"readingDate": {
"$date": {
"$numberLong": "1656295562871"
}
},
"spotPrice": {
"$numberDouble": "0.18317855743872674"
}
},
{
"readingDate": {
"$date": {
"$numberLong": "1656381961889"
}
},
"spotPrice": {
"$numberDouble": "0.253926321676319"
}
},
{
"readingDate": {
"$date": {
"$numberLong": "1656468400214"
}
},
"spotPrice": {
"$numberDouble": "0.23309041730430674"
}
}
]
}
...但我在寻找
...,
{
"vaultId": "552",
"vaultName": "RR/BAYC",
"vaultContract": "0xcd2e3a66507e94190e3b1521a189ad821c8c3006",
"prices": [
{
"readingDate": "1656295562871",
"spotPrice": "0.18317855743872674"
},
{
"readingDate": "1656381961889",
"spotPrice": "0.253926321676319"
},
{
"readingDate": "1656468400214",
"spotPrice": "0.23309041730430674"
}
]
},
...我添加了ejson作为应用程序的依赖项,并添加了以下内容
requestResponse = await context.services
.get("mongodb-atlas")
.db("mydb")
.collection("mycollection")
.aggregate(pipeline);
let newData = await EJSON.stringify(requestResponse);
return newData;
};但我回来了
> result:
"{}"
> result (JavaScript):
EJSON.parse('"{}"')我肯定我错过了一些明显的东西。
发布于 2022-06-29 21:59:34
不知道你做了什么,但这件事是现成的:
函数ejson
exports = function(arg){
return context.services.get("mongodb-atlas")
.db("so")
.collection("SO72801121")
.find({})
};

具有json输出的端点使用ejson函数:

结果:https://us-east-1.aws.data.mongodb-api.com/app/application-0-ioxbi/endpoint/SO72801121

https://stackoverflow.com/questions/72801121
复制相似问题