I am using M0 free tier shared cluster for some Mongo DB POC. I am trying to aggregate data from a Time Series collection (Events_TS )and putting the output of the aggregation pipeline into another collection(Aggreated_Events). For that, I found that The Data API needs to hit a custom HTTPS endpoint as a System user. So I have created a custom HTTPS endpoint with a function. The function looks like below
Function:-
exports = async function (request, response) {
const mongo = context.services.get("PDCCluster");
collection = mongo.db("PDC").collection("Events_TS");
const pipeline = [{
"$group": {
"_id": {
"ProblemCorrelation": "$ProblemCorrelation",
"pyTextValue(1)": {
"$substr": [
{
"$cond": [
{
"$ne": ["$GeneratedDateTime", null]
},
{
"$toString": "$GeneratedDateTime"
},
""
]
},
0,
13
]
},
"RuleApplication": "$RuleApplication",
"pyTextValue(2)": {
"$substr": ["$requestorid", 0, 1]
},
"MsgID": "$MsgID",
"ClusterName": "$ClusterName"
},
"pySummaryValue(1)": {
"$sum": "$KPIValue"
},
"pySummaryCount(1)": {
"$sum": 1
}
}
},
{
"$sort": {
"pyTextValue(1)": 1,
"_id.ProblemCorrelation": 1,
"_id.pyTextValue(2)": 1,
"_id.RuleApplication": 1,
"_id.MsgID": 1,
"_id.ClusterName": 1
}
}
] ;
documents = await collection.aggregate(pipeline).toArray();
return {documents}}
URL which I am trying to hit from Data API:-
https://ap-south-1.aws.data.mongodb-api.com/app/data/endpoint/PDCAggregate
The JSON which I am trying to pass thru Data APi as requested:-
{
"collection":"Events_TS",
"database":"PDC",
"dataSource":"PDCCluster",
"pipeline": [
{
"$match": {
"$and": [
{
"GeneratedDateTime": {
"$gte": "2023-06-01T07:00:20.435Z",
"$lt": "2023-06-20T07:20:20.435Z"
}
}
]
}
},
{ "$merge" : { "into" : { "coll": "Aggreated_Events" }, "on": "_id", "whenNotMatched": "insert" } }]}
But on sending the above JSON as a request to the URL I am getting 204 no content response. Please help me out here.