0

I have the JSON below in mongodb and would like write a bson.M filter to get a specific JSON in collection.

JSONs in collection:

{
    "Id": "3fa85f64",
    "Type": "DDD",
    "Status": "PRESENT",
    "List": [{
        "dd": "55",
        "cc": "33"
    }],
    "SeList": {
        "comm_1": {
            "seId": "comm_1",
            "serName": "nmf-comm"
        },
    "comm_2": {
            "seId": "comm_2",
            "serName": "aut-comm"
        }
    }
}

{
    "Id": "3fa8556",
    "Type": "CCC",
    "Status": "PRESENT",
    "List": [{
        "dd": "22",
        "cc": "34"
    }],
    "SeList": {
        "dnn_1": {
            "seId": "dnn_1",
            "serName": "dnf-comm"
        },
    "dnn_2": {
            "seId": "dnn_2",
            "serName": "dn2-comm"
        }
    }
}

I have written below the bson.M filter to select the first JSON but did not work because I do not know how to handle the map keys in the "SeList.serName". The keys comm_1, comm_2, dnn_1, etc could be any string.

filter := bson.M{"Type": DDD, "Status": "PRESENT", "SeList.serName": nmf-comm} // does not work because the "SeList.serName" is not correct.

I need help about how to select any JSON based on the example filter above.

prasad_
  • 12,755
  • 2
  • 24
  • 36
van
  • 77
  • 1
  • 6
  • `SeList.serName` - you can use `SeList.dnn_1.serName` _or_ `SeList.dnn_2.serName` to refer the field. – prasad_ Jun 29 '21 at 09:20
  • You can try this approach - when you don't know the key names in a map but you need to access the sub-map fields: [How to control the projection definition in MongoDb using C#](https://stackoverflow.com/questions/67972956/how-to-control-the-projection-definition-in-mongodb-using-c-sharp). – prasad_ Jun 29 '21 at 09:27

0 Answers0