MongoDB: How to get distinct values from a nested field within an Array starting with a dollar sign?

Viewed 16

I'm currently using the MongoDB Database Profiler and want to Query the system.profile Collection to read distinct values of the field tenantId in our profiled aggregations.

The BSON-Documents looks like this:

    {
    "op" : "command",
    ...
    "command" : {
        ...
        "query" : {
            "$and" : [ 
                {
                    "type" : {
                        "$in" : [ ... ]
                    }
                }, 
                {
                    "tenantId" : "1234567890",
                    "shardKey" : "a8"
                }
            ]
        }
...
}
 {
    "op" : "command",
    ...
    "command" : {
        ...
        "query" : {
            "$and" : [ 
                {
                    "type" : {
                        "$in" : [ ... ]
                    }
                }, 
                {
                    "tenantId" : "0987654321",
                    "shardKey" : "a8"
                }
            ]
        }
...
}

I tried the following: db.system.profile.distinct("command.query.$and[1].tenantId") which is not working because of:

"FieldPath field names may not start with '$'.",

I also tried a lot of other different things like huge aggregation pipelines which won't work. I just can't figure it out.

I'm currently on MongoDB Version 4.4, so I couldn't test $getField.

Expected Output would be: ["1234567890","0987654321"] and of course no duplicates.

I hope someone can help me :)

Kind regards, Mildem

0 Answers
Related