I have JSON documents that collect metadata about specific searches within a SaaS application. Here's a sample document:
{
"startSearch": "2021-04-23T15:12:59.161Z",
"endSearch": "2021-04-23T15:13:00.501Z",
"elapsedTime": 1.34,
"totalRecordsFound": 224,
"specialSearch": "Test",
"AccountStatus": "Active, Bankruptcy, Out For Repo, Repossessed",
"AccountType": "",
"Location": "",
"LastName": "",
"FirstName": "",
"AccountId": "",
"Phone": ""
}
As you can see, the AccountStatus field is a comma-delimited string that should probably be an array. I would like to analyze search results based on how long such an array might be. The pseudocode of such a solution would be something like this:
db.getCollection('Baselining').aggregate([
{
$match: {
"specialSearch": { $ne: "" },
"AccountStatus.split(',').length": { $gt: 1 }
}
},
{
$group: {
_id: null,
averageSearchDuration: { $avg: "$elapsedTime"}
}
}
])
Two questions, then: how can I convert the comma-delimited fields into arrays, and how can I group the aggregation by the length of the array? TIA for your help!