Stitch filter incoming post webhook payload

Hi, I’m working on an incoming webhook post request in Stitch and have a question about filtering the payload please – is it possible to filter the payload so that only a select field is inserted into the database collection?
I’ve used a mongodb youtube tutorial as the basis, which is great and uses an async/await function to insert the entire payload with “insertOne” – but I only want to insert a specific field (e.g. “name”:”John”) from the this payload into the database.
Many thanks, Andi

PS here is the function I’m using

exports = async function(payload, response) {
    const mongodb = context.services.get("mongodb-atlas");
    const eventsdb = mongodb.db("mydatabase");
    const eventscoll = eventsdb.collection("mycollection");
    const result= await eventscoll.insertOne(payload.query);
    var id = result.insertedId.toString();
    if (result) {
        return JSON.stringify(id,false,false);
    }
    return { text: `Error saving` };
}

Hi @a_Jn, welcome!

For POST webhook, you could retrieve a specific field as below:

const body = payload.body.text(); 
const document = EJSON.parse(body); 
console.log("Only Name Field:", document.name);

Please note that this is the value (string), if you would like to insert this as a document you would need to create a document. i.e. {"name": document.name}.
For more information see Stitch: Incoming WebHooks

Regards,
Wan.

1 Like

Great thank you so much Wan,

I’m a total newbie (as you can probably tell=) and trying out Mongo to use as nosql database for future applications.

Yes, I’d like to insert just the name field as a new document but I’m struggling to integrate {“name”: document.name} into that function, which is currently working really well to insert the entire payload as a new document.

I also have one last question please, to see how to make use of a GET webhook – where we only want to show the id field (not the _id):

[{“_id”:{“$oid”:“5e83219bea6a9407daef5b77”},“id”:“3456787”},{“_id”:{“$oid”:“5e8322a71a4ca4049533353b”},“id”:“3456789”},{“_id”:{“$oid”:“5e8322df1ef1af501adbcc9b”},“id”:“3456788”}]

https://webhooks.mongodb-stitch.com/api/client/v2.0/app/app1-ifvea/service/httpGET/incoming_webhook/webhookhttpGET

PS here is the function I’m using, I’m struggling with the last line, as to only show the id:

exports = function(payload) {
const mongodb = context.services.get(“mongodb-atlas”);
const mycollection = mongodb.db(“mydatabase”).collection(“mycollection”);
return mycollection.find({}).toArray();
};

Many thanks !

Andi

1 Like

Hi @a_Jn,

You can use projection to exclude the _id field, for example:

return collection.find({}, {_id:0}).toArray();

See also Stitch: collection.find(), especially the projection options.

Not a problem, we are all learning :slight_smile: . You may find MongoDB Stitch Tutorials useful as well.

Regards,
Wan.

1 Like

Thank you so much Wan, that projection works really well for excluding a field, I’m going to dig deeper into the Stitch tutorials, looks like it has super potential for apps! Many thanks Andi

1 Like