Counting occurrences of a given field value inside a document

Hey there,

im doing my first tries with MongoDB and im actually hitting a wall. Im not sure if I misunderstood the concept behind MongoDB or if I just have issues getting up and running.

Im importing data from an API and im saving then as that kind of documents (its data from an online game, so no worries that the field names are little weird)

{
    "_id" : ObjectId("626abbdff6df0a095e8d66d0"),
    "identifier" : "2022-04-28T17:22:41_Pushy Tanky_Doctorfeelgood",
    "createdAt" : ISODate("2022-04-28T16:07:59.068+0000"),
    "damageRegion" : NumberInt(14),
    "killer" : {
        "_id" : ObjectId("000000000000000000000000"),
        "createdAt" : ISODate("0001-01-01T00:00:00.000+0000"),
        "updatedAt" : ISODate("0001-01-01T00:00:00.000+0000"),
        "clanId" : NumberInt(0),
        "clanLevel" : NumberInt(0),
        "factionId" : NumberInt(0),
        "factionSympathy" : NumberInt(0),
        "name" : "Pushy Tanky",
        "class" : "",
        "combatRank" : NumberInt(67),
        "faction" : "CityAdmin",
        "profession" : "",
        "characterSlot" : "",
        "clan" : {
            "_id" : ObjectId("000000000000000000000000"),
            "createdAt" : ISODate("0001-01-01T00:00:00.000+0000"),
            "updatedAt" : ISODate("0001-01-01T00:00:00.000+0000"),
            "factionSympathy json:" : NumberInt(0),
            "money" : NumberInt(0),
            "name" : "-17th squad-",
            "shortname" : "17th squad"
        }
    },
    "target" : {
        "_id" : ObjectId("000000000000000000000000"),
        "createdAt" : ISODate("0001-01-01T00:00:00.000+0000"),
        "updatedAt" : ISODate("0001-01-01T00:00:00.000+0000"),
        "clanId" : NumberInt(0),
        "clanLevel" : NumberInt(0),
        "factionId" : NumberInt(0),
        "factionSympathy" : NumberInt(0),
        "name" : "Doctorfeelgood",
        "class" : "",
        "combatRank" : NumberInt(57),
        "faction" : "Black Dragon",
        "profession" : "",
        "characterSlot" : "",
        "clan" : {
            "_id" : ObjectId("000000000000000000000000"),
            "createdAt" : ISODate("0001-01-01T00:00:00.000+0000"),
            "updatedAt" : ISODate("0001-01-01T00:00:00.000+0000"),
            "factionSympathy json:" : NumberInt(0),
            "money" : NumberInt(0),
            "name" : "",
            "shortname" : ""
        }
    },
    "timeStamp" : "2022-04-28T17:22:41",
    "updatedAt" : ISODate("2022-04-28T16:07:59.068+0000"),
    "weapon" : "RAVAGER",
    "weaponResourceKey" : "2040470A",
    "world" : "TUNNEL I"
}

I managed to filter out duplicates where target.name and killer.name is of the same value. What im now trying to do, is basically to query a sorted view. Meaning, I want for every unique killer.name to count its occurrences in killer.name and target.name. I understand, that this will mean to transform to a new document but im having issues to get the “dynamic filtering” going, as I need to have the unique names first.

Does it make sense? Am I trying something which is not possible with MongoDB that easily? Its certainly possible that I did a wrong decision.

Thanks for your help :slight_smile:

Hi @SantoDE ,

It seems that your documents have only embedded docs and no arrays so it sounds that you just need to use $group on “killer.name” and/or “target.name” with a $count or $sum(1) operator ( do the same eventually)

Does that work for you?

Thanks
Pavel

Hello @Pavel_Duchovny,

thanks for the reply :slight_smile: I feel like im missing something. Do you envision that working in one group phase or in two? Is the _id then each respective value? I think it is. And then, latest, to add a count to (each?) group stage?