Querying Federated Database Instance from Functions #2

Hi everyone.
I’m using Atlas MongoDB. I have added a data source starting from a HTTP(S) URL pointing to a CSV file and I have create a collection inside a VirtualDatabase. Everything works well infact I’m able to query this collection both from Compass or with custom code from VS Code. Unfortunately I can’t read data from inside a linked function associated to an HTTP Endpoint. This is the source of my function:
exports = async function() {
const serviceName = ‘mongodb-atlas’;
const myDataLake = context.services.get(serviceName);
const myDB = myDataLake.db(‘VirtualDatabase0’);
return await myColl.find({first_name: “Lissie”});
}
Unfortunately the result is always an empty collection (the query is right - tested on VS Code).
Hoping someone can help me.

Bye.
John

Is this missing myColl? When I tried to execute your lines of code but with reference to my own test data sources, I got back the following error:

{“message”:“‘myColl’ is not defined”,“name”:“ReferenceError”}

You have a link to the database VirtualDatabase0 but the query is being run without a collection specified.

I wrote something similar on my test environment function which had included a reference to an existing collection in the federated instance which returned an expected document:

exports = async function() {
  const serviceName = 'fdi';
  const myDataLake = context.services.get(serviceName);
  const myDB = myDataLake.db('airbnb');
  const myColl = myDB.collection('listingsAndReviews') /// <--- Additional line referencing the collection name
  return await myColl.findOne({});
}

result from the above function being run in my test environment:

> result: 
{
  "_id": "23715192",
  "listing_url": "https://www.airbnb.com/rooms/23715192",
  "name": "Porto Downtown Luxury Studio",
  ...
}

Note: shortened the result for readability

Regards,
Jason

Also quickly did a find() (instead of findOne()) for the same document which ran as well:

exports = async function() {
  const serviceName = 'fdi';
  const myDataLake = context.services.get(serviceName);
  const myDB = myDataLake.db('airbnb');
  const myColl = myDB.collection('listingsAndReviews')
  return await myColl.find({'_id':'23715192'});
}

Result:

> result (JavaScript): 
EJSON.parse('[{"_id":"23715192","listing_url":"https://www.airbnb.com/rooms/23715192","name":"Porto Downtown Luxury Studio",...

Dear Jason,
thanks a lor for your prompt reply.
In the operation of cut&paste of my code snippet I forgot the line:

const myColl = db.Collection(‘Test_CSV_Collection’);

Anyway, the result is always the same either with find() and findOne() method, with or without filters. This is my result obtained by using “Run” from the funciont edito window:

ran at 1688980963940
took 529.584588ms
result:

result (JavaScript):
EJSON.parse(‘’)

I noticed that you call your service name as ‘fdi’. In my case the service name is ‘mongdb-atlas’. While this linked datasource of my app has is pointing to the “cluster” instance, I created a new linked source that refers to the Federated Database Instance that, infact, is the environment where the external data source is linked.
By changing the service name everything worked.

I appreaciated your help. Thanks a lot. :smile:

1 Like

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