You category dict will not fit in mongo as mongo requires objects to have keys in string types. You can wrangle the dict to below form for easier processing:
```json
[
{
category: 1,
fruits: [
"apple"
]
},
{
category: 2,
fruits: [
"banana",
"kiwi"
]
},
{
category: 3,
fruits: [
"pineapple",
"strawberry"
]
}
]
```
In aggregation pipeline, iterate through the wrangled category array by `$reduce` to conditionally update the accumulator to get the max matched category.
```js
db.collection.aggregate([
{
"$unwind": "$shops"
},
{
"$set": {
"max_category": {
"$reduce": {
"input": [
{
category: 1,
fruits: [
"apple"
]
},
{
category: 2,
fruits: [
"banana",
"kiwi"
]
},
{
category: 3,
fruits: [
"pineapple",
"strawberry"
]
}
],
"initialValue": null,
"in": {
"$cond": {
"if": {
$and: [
{
$gt: [
"$$this.category",
"$$value"
]
},
{
$gt: [
{
$size: {
"$setIntersection": [
"$$this.fruits",
"$shops.fruits"
]
}
},
0
]
}
]
},
"then": "$$this.category",
"else": "$$value"
}
}
}
}
}
},
{
"$group": {
"_id": "$_id",
"country": {
$first: "$country"
},
"max_category": {
$max: "$max_category"
},
"shops": {
"$push": "$shops"
}
}
}
])
```
[Mongo Playground](https://mongoplayground.net/p/sf1u2N0G7Hv)
You are specifying the `inlineTemplate.envelope` property incorrectly. You can define the recipients and custom fields directly within the inlineTemplate. You do not have to specify the emailSubject/emailBlurb within the inlineTemplate.
Also note that the custom fields specified at the root level will be ignored when using composite templates. See this [answer][1]
The following json should work for you.
{
"emailSubject": "Sent from Node SDK",
"emailBlurb": "Email body here",
"status": "sent"
"compositeTemplates": [
{
"inlineTemplates": [
{
"sequence": "1",
"documents": [
{
"documentBase64": "base64StringHere",
"documentId": "1",
"fileExtension": ".pdf",
"name": "filename.pdf"
}
],
"customFields": {
"textCustomFields": [
{
"name": "DSFSSourceObjectId",
"required": false,
"show": false,
"value": "dealIdHere"
}
]
},
"recipients": {
"signers": [
{
"email": "myEmail@domain.com",
"name": "My Name",
"recipientId": "1"
}
]
}
}
]
}
]
}
[1]: https://stackoverflow.com/a/42153369/1219543