I have a use case where I want to send the contents of a csv file to a mongoDB collection whenever the file is modified. I found that a webhook could be created in mongoDB Realm. The intention of code below is to do 2 things. First, drop a specified collection in a specified db. Second, to insert many (~10k+) documents to the specified collection.
exports = function(payload, response) {
const {database, coll_to_update} = payload.query;
const contentTypes = payload.headers["Content-Type"];
const body = payload.body;
console.log("database, coll_to_update:", database, coll_to_update);
console.log("Content-Type:", JSON.stringify(contentTypes));
console.log("Request body:", body);
const coll = context.services.get("mongodb-atlas").db(database).collection(coll_to_update);
coll.deleteMany({})
.then(result => console.log(`Deleted ${result.deletedCount} item(s).`))
.catch(err => console.error(`Delete failed with error: ${err}`))
coll.insertMany(body)
.then(result => console.log(`Successfully inserted ${result.insertedIds.length} items!`))
.catch(err => console.error(`Failed to insert documents: ${err}`))
return payload;
};
This is being written in the Function editor in the online Realm UI. Because I could not find a way to drop the collection, I tried to delete all documents from it by passing an empty query. But I get an error saying FunctionError: mongodb delete: no arguments were passed. I am able to delete documents if I provide a query. Is there any query that would always match all documents I could use, or a better way to drop or delete all in a collection?
The second issue is I am not sure how to decode the contents of the csv sent in the request body. The curl I am using is below. Just for testing, I also sent it to http://httpbin.org/post and the json is decoded correctly as an array of two objects:
curl -H "Content-Type: application/json" -d [{\"foo\":\"bar\"},{\"baz\":\"zap\"}] \
"https://eu-west-1.aws.webhooks.mongodb-realm.com/api/client/v2.0/app/application-0- \
abcdef/service/mongo_doodah/incoming_webhook/webhook0? \
database=FIC&coll_to_update=FIC_data&secret=not_this_one"
When sending to the Realm endpoint however, I get error FunctionError: mongodb insert: argument must be an array. Checking the logs I see:
Logs:
[
"database, coll_to_update: FIC FIC_data",
"Content-Type: [\"application/json\"]",
"Request body: [object Binary]"
]
Body:
{
"$binary": {
"base64": "W3siZm9vIjoiYmFyIn0seyJiYXoiOiJ6YXAifV0=",
"subType": "00"
}
}
So Realm is working differently to, for example, pastebin in how it handles the json I sent. I cannot figure out how to get the json I sent out of this binary object within the Realm webhook editor.