Cant read data from collection in MongoDB Atlas Trigger
NickName:Warlax56 Ask DateTime:2021-09-22T13:59:13

Cant read data from collection in MongoDB Atlas Trigger

New to MongoDB, very new to Atlas. I'm trying to set up a trigger such that it reads all the data from a collection named Config. This is my attempt:

exports = function(changeEvent) {
  const mongodb = context.services.get("Cluster0");
  const db = mongodb.db("TestDB");
  var collection = db.collection("Config");
  config_docs = collection.find().toArray(); 
  console.log(JSON.stringify(config_docs));
}

the function is part of an automatically created realm application called Triggers_RealmApp, which has Cluster0 as a named linked data source. When I go into Collections in Cluster0, TestDB.Config is one of the collections.

Some notes:

  • it's not throwing an error, but simply returning {}.
  • When I change context.services.get("Cluster0"); to something else, it throws an error
  • When I change "TestDB" to a db that doesnt exist, or "Config" to a collection which doesn't exist, I get the same output; {}
  • I've tried creating new Realm apps, manually creating services, creating new databases and new collections, etc. I keep bumping into the same issue.
  • The mongo docs reference promises and awaits, which I haven't seen in any examples (link). I tried experimenting with that a bit and got nowhere. From what I can tell, what I've already done is the typical way of doing it.

Images: Collection: TestDB database and Config collection Linked Data Source: Linked data source, Cluster0

Copyright Notice:Content Author:「Warlax56」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69278930/cant-read-data-from-collection-in-mongodb-atlas-trigger

Answers
Warlax56 2021-10-14T20:41:34

I ended up taking it up with MongoDB directly, .find() is asynchronous and I was handling it incorrectly. Here is the reply straight from the horses mouth:\n\nAs I understand it, you are not getting your expected results from the query you posted above. I know it can be confusing when you are just starting out with a new technology and can't get something to work!\nThe issue is that the collection.find() function is an asynchronous function. That means it sends out the request but does not wait for the reply before continuing. Instead, it returns a Promise, which is an object that describes the current status of the operation. Since a Promise really isn't an array, your statment collection.find().toArray() is returning an empty object. You write this empty object to the console.log and end your function, probably before the asynchronous call even returns with your data.\nThere are a couple of ways to deal with this. The first is to make your function an async function and use the await operator to tell your function to wait for the collection.find() function to return before continuing.\nexports = async function(changeEvent) {\n const mongodb = context.services.get("Cluster0");\n const db = mongodb.db("TestDB");\n var collection = db.collection("Config");\n config_docs = await collection.find().toArray(); \n console.log(JSON.stringify(config_docs));\n\n};\n\nNotice the async keyword on the first line, and the await keyword on the second to last line.\nThe second method is to use the .then function to process the results when they return:\nexports = function(changeEvent) {\n const mongodb = context.services.get("Cluster0");\n const db = mongodb.db("TestDB");\n var collection = db.collection("Config");\n collection.find().toArray().then(config_docs => {\n console.log(JSON.stringify(config_docs));\n });\n};\n",


More about “Cant read data from collection in MongoDB Atlas Trigger” related questions

Cant read data from collection in MongoDB Atlas Trigger

New to MongoDB, very new to Atlas. I'm trying to set up a trigger such that it reads all the data from a collection named Config. This is my attempt: exports = function(changeEvent) { const mongo...

Show Detail

Cant read data from collection in MongoDB Atlas Trigger

New to MongoDB, very new to Atlas. I'm trying to set up a trigger such that it reads all the data from a collection named Config. This is my attempt: exports = function(changeEvent) { const mongo...

Show Detail

Save instances from Mongodb collection as string variables in an Atlas Trigger Function

I am trying to set up a MongoDB Atlas trigger function that searches a mongoDB database to collect string variables, then uses those as the to log into a separate API. When I run this function: exp...

Show Detail

How to read data from mongodb atlas from pyspark?

I am trying to read data from mongodb atlas from pyspark but it is throwing an error. Py4JJavaError: An error occurred while calling o204.load. : java.lang.IllegalArgumentException: requirement fai...

Show Detail

How to create an Insert trigger in mongodb atlas?

I am new to NoSQL/MongoDB and I need to write a trigger on insert event on one of the collection in my database as follow: Database Name: GiftShop Collection Name: Gifts Gifts Schema is as Follo...

Show Detail

Getting NULL data from collection from MongoDB Atlas

I have connected my code to MongoDB Atlas using Mongoose... even though the collection has a data in it its shows null in response. I want to know the exact issue and troubleshoot it, because the

Show Detail

MongoDB Atlas - Trigger returning empty object

I've been trying to search through documentation, and other various stack overflow posts, but I can't seem to find out how to do this. I'm trying setup a Trigger for MongoDB Atlas with the following

Show Detail

Fetching Sample Data from MongoDB Atlas with mongoose

As the title suggests, I am trying to fetch existing data that already exists inside MongoDB Atlas. The data comes from the mongoDB Atlas sample data, and I am using the sample_mflix database. I h...

Show Detail

Azure Data Factory - delete data from a MongoDb (Atlas) Collection

I'm trying to use Azure Data Factory (V2) to copy data to a MongoDb database on Atlas, using the MongoDB Atlas connector but I have an issue. I want to do an Upsert but the data I want to copy has no

Show Detail

Cant connect to specific database and collection inside mongoDB Atlas Cluster

I am creating a MERN stack application and have chosen to use mongoose to communicate with MongoDB Atlas. But MongoDB Atlas uses clusters with databases inside which again has collections. I cant f...

Show Detail