Using The Embedded Document Database
The Embedded Document Database provides an easy to use way to store and retrieve data without having to configure or use an external database.
Store Incoming Email Addresses
This example shows how to store and retrieve data using the Embedded Document DB. Suppose we want to maintain a database of incoming email addresses along with a count of how many emails have been received for each address.
We will use a database name 'EmailSenders' with a collection name 'Incoming'. The database & collection will be automatically created when the first record is added.
Each document in the collection will be simple Json:
{
"Name": "Andrew Bentley",
"Email": "andrew@ctestcompany.co.uk",
"LastDate": "2021-09-22T12:14:23Z",
"LastSubject": "Top 10 new vehicle leasing offers",
"ReceivedCount": 1
}
We first use the Embedded Data Store action to lookup an existing document using a SQL SELECT statement:
The @Email parameter value is set to the built-in field %Msg_FromEmail%.
This will return a Json document:
... if a document is found, or blank otherwise.
The results are returned to the %ExistingJson% variable.
If the %ExistingJson% variable is not blank (IE: A document was returned). Then we use the Extract Field action to extract the _id and ReceivedCount fields. We then use the Set action to Increment the %ExistingCount% variable.
We then use the Embedded Data Store action to update the existing document, using a SQL UPDATE statement:
If a document was not returned from the first lookup we insert a new one.
We use the Create Json action to create our Json:
and then the Embedded Data Store action to insert it:
This Automation could be called from an email source (Office 365, Gmail etc). When each new email arrives the EmailSenders database will be updated.
After processing some messages the EmailSenders 'Incoming' collection will contain a document for each unique incoming address along with a count of emails received and the last subject & date.
This database could be queried on any other Automations. For example: to get a list of the top 10 email senders you would use:
This can be returned as a Json array or as CSV.