最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cant read data from collection in MongoDB Atlas Trigger - Stack Overflow

programmeradmin1浏览0评论

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: Linked Data Source:

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: Linked Data Source:

Share Improve this question edited Sep 24, 2021 at 20:00 Warlax56 asked Sep 22, 2021 at 5:59 Warlax56Warlax56 1,2121 gold badge11 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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:


As 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!

The 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.

There 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.

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

};

Notice the async keyword on the first line, and the await keyword on the second to last line.

The second method is to use the .then function to process the results when they return:

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

The connection has to be a connection to the primary replica set and the user log in credentials are of a admin level user (needs to have a permission of cluster admin)

发布评论

评论列表(0)

  1. 暂无评论