Return JSON instead of EJSON

I have created a simple Realm App that will use a HTTPS endpoint to run a function which runs an aggregation pipeline.

The function looks like this…

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

return requestResponse;
};

When setting up the HTTPS endpoint I have the option of choosing the return type as JSON or EJSON. When choosing EJSON it works fine but includes additional details in the response that are not required. When switching to JSON the response fails.

At the moment the response looks like this…

...
,
  {
    "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"
        }
      }
    ]
  }
...

but I’m looking for it to be

...,
  {
    "vaultId": "552",
    "vaultName": "RR/BAYC",
    "vaultContract": "0xcd2e3a66507e94190e3b1521a189ad821c8c3006",
    "prices": [
      {
        "readingDate": "1656295562871",
        "spotPrice": "0.18317855743872674"
      },
      {
        "readingDate": "1656381961889",
        "spotPrice": "0.253926321676319"
      },
      {
        "readingDate": "1656468400214",
        "spotPrice": "0.23309041730430674"
        }
    ]
  },
...

If I change to

requestResponse = await context.services
    .get("mongodb-atlas")
    .db("nftx_aggregators")
    .collection("spot_price")
    .aggregate(pipeline);
    
    let jsonData = JSON.stringify(requestResponse);


    return jsonData;
};

I get back

> result: 
"{}"
> result (JavaScript): 
EJSON.parse('"{}"')

I then added toArray() to get that working

requestResponse = await context.services
    .get("mongodb-atlas")
    .db("nftx_aggregators")
    .collection("spot_price")
    .aggregate(pipeline).toArray();
    
    let jsonData = JSON.stringify(requestResponse);


    return jsonData;
};

That returns the following

> result: 
"[{\"vaultId\":0,\"vaultName\":\"CryptoPunks\",\"vaultContract\":\"0x269616d549d7e8eaa82dfb17028d0b212d11232a\",\"prices\":[

… and although it’s missing the "$numberInt" and other EJSON items the format isn’t right.

How do I go about getting the response to look like

...,
  {
    "vaultId": "552",
    "vaultName": "RR/BAYC",
    "vaultContract": "0xcd2e3a66507e94190e3b1521a189ad821c8c3006",
    "prices": [
      {
        "readingDate": "1656295562871",
        "spotPrice": "0.18317855743872674"
      },
      {
        "readingDate": "1656381961889",
        "spotPrice": "0.253926321676319"
      },
      {
        "readingDate": "1656468400214",
        "spotPrice": "0.23309041730430674"
        }
    ]
  },
...

Hi @NFTX_Tech and welcome back :muscle: !

Try this instead. Use the response parameter and set the body:

coll.find(query, project).sort(sort).toArray()
    .then( docs => {
      response.setBody(JSON.stringify(docs));
    });

You don’t need to return anything.

Cheers,
Maxime.