#StackBounty: #javascript #mongodb #realm Deleting collection and inserting array of docs via mongoDB Realm webhook

Bounty: 50

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);
      .then(result => console.log(`Deleted ${result.deletedCount} item(s).`))
      .catch(err => console.error(`Delete failed with error: ${err}`))
      .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"}] 

When sending to the Realm endpoint however, I get error FunctionError: mongodb insert: argument must be an array. Checking the logs I see:

  "database, coll_to_update: FIC FIC_data",
  "Content-Type: ["application/json"]",
  "Request body: [object Binary]"

  "$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.

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.