Brian Behe
Brian Behe

Reputation: 542

How to Generate Counts of Elements Returned from Map Function?

I have a map function

function (doc) {
    for(var n =0; n<doc.Observations.length; n++){
        emit(doc.Scenario, doc.Observations[n].Label);
    } 
}

the above returns the following:

{"key":"Splunk","value":"Organized"},
{"key":"Splunk","value":"Organized"},
{"key":"Splunk","value":"Organized"},
{"key":"Splunk","value":"Generate"},
{"key":"Splunk","value":"Ingest"}

I"m looking to design a reduce function that will then return the counts of the above values, something akin to:

Organized: 3
Generate: 1
Ingest: 1

My map function has to filter on my Scenario field, hence why I have it as an emitted key in the map function.

I've tried using a number of the built in reduce functions, but I end up getting count of rows, or nothing at all as the functions available don't apply.

I just need to access the counts of each of the elements that appear in the values field. Also, the values present here are representative, there could 100s of different types of values found in the values field for what that's worth.

I really appreciate the help!

Here's sample input:

{
    "_id": "dummyId",
    "test": "test",
    "Team": "Alpha",
    "CreatedOnUtc": "2019-06-20T21:39:09.5940830Z",
    "CreatedOnLocal": "2019-06-20T17:39:09.5940830-04:00",
    "Participants": [
        {
            "Name": "A",
            "Role": "Person"
        }
    ],
    "Observations": [
        {
           "Label": "Report",
        },
        {
            "Label": "Ingest",
        },
        {
            "Label": "Generate",
        },
        {
            "Label": "Ingest",
        }
     ]
}

Upvotes: 1

Views: 29

Answers (1)

Siddhant Aggarwal
Siddhant Aggarwal

Reputation: 19

You can set the map by "value" as your key and associate an increment to that key to make sure a count is maintained. And then you can print your map which should look as you are requesting for.

Upvotes: 1

Related Questions