Get data from mongo cluster to realm function

I am trying to get data from collection to realm function but getting only object object

     const cluster = context.services.get("mongodb-atlas");
   const users = cluster.db("ssn").collection("invites");
   const stories = cluster.db("ssn").collection("stories");
   const twilio = context.services.get("Twilio");
   
   
   const story = await stories.find({});
 console.log(story);

Hi @Jayesh_Sharma,

You are missing the toArray() and the parsing:

Try this:

exports = function(arg){
    const collection = context.services.get("mongodb-atlas").db("test").collection("test");
    collection.find({}).toArray().then((docs) => {
      console.log(EJSON.stringify(docs));
    });
};

Cheers,
Maxime.

1 Like

hii @MaBeuLux88 , it displays only first data not all.

My function above prints all the documents in the test.test collection.

Make sure you are not using findOne() instead of find() or something like that. Also the query might be filtering more than you think?

@MaBeuLux88 , I am using the same you have told me.

Humm I know that the output of the console.log is trimmed when it’s too long. Maybe that’s it?

If you create a REST API instead and return the docs in the body of the response, it looks like this:

exports = function(payload, response) {
    const coll = context.services.get("mongodb-atlas").db("test").collection("test");
    coll.find().toArray().then( docs => {
      response.setBody(EJSON.stringify(docs));
    });
};

And the answer I get from the REST API looks like this:

[{"_id":{"$oid":"61536690650d531e1c196bdd"},"date":{"$date":{"$numberLong":"1293922800000"}}},{"_id":{"$oid":"616d5221523ab7510f03522c"},"to":"maxime"}]

Or like this

[{"_id":"61536690650d531e1c196bdd","date":"2011-01-01T23:00:00.000Z"},{"_id":"616d5221523ab7510f03522c","to":"maxime"}]

It depends if I use the EJSON (first output) or JSON (second one) help to stringify my docs.

To create a REST API, head into the 3rd Party Service > HTTP.

Cheers,
Maxime.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.