Home > OS >  Mongodb - Unable to query multiple documents in Realm Trigger
Mongodb - Unable to query multiple documents in Realm Trigger

Time:11-25

I am using Real Scheduled Triggers to run some background job of data validation. In the Trigger function i want to query all the documents in the collection, but that doesn't work. But when i try to findOne document i get the response.

//Does't work, returns blank

const collection =  context.services.get("Cluster0").db("testdocument").collection("testcollection");
return collection.find({}).then((result)=>{  console.log('data : ',JSON.stringify(result));     })

//Works, returns the result

const collection =  context.services.get("Cluster0").db("testdocument").collection("testcollection");

return collection.find({isActive:'true'}).then((result)=>{ console.log('data :',JSON.stringify(result)); })

Can anyone please suggest if i am doing anything wrong here.

CodePudding user response:

I think that your question used the same code for both //doesn't work and //works. But I think that what worked is findOne, that is because it returns a document while find returns a cursor. You have to use .toArray() before .then() as shown in the documentation but not obvious in any way.

https://docs.mongodb.com/realm/mongodb/actions/collection.find/

  • Related