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:
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