I'm writing a function in MongoDB Realm that I want to use as a custom resolver for GraphQL. Part of the function should get a specific document by its id. According to the documentation I can get access to the Atlas DB by using context.services.get('mongodb-atlas')
. Here's the simplified code extract:
exports = async () => {
const cluster = context.services.get('mongodb-atlas');
const collection = cluster.db('my-db').collection('my-coll');
return await collection.findOne({"_id": "61d33d2059c38ef0584c94f8"});
}
However, the findOne()
call returns null
. I know that the id above is valid, because I can successfully call collection.find()
and list all the documents with their ids.
What I've tried
findOne({"_id": "61d..."})
- returnsnull
findOne({"_id": new ObjectId("61d...")})
- error: "ObjectId" is not defined.- Explicitly declaring ObjectId with
const ObjectId = require('mongodb').ObjectId
as recommended here - error: "MongoDB Node.js Driver is not supported". - Declaring ObjectId with
const ObjectId = require('bson').ObjectId
-findOne()
returnsnull
again.
Can you recommend any other avenue by which to do this lookup?