I have an array of JSON objects as
Structs:
type Data struct {
TaiList []Tai `json:"taiList"`
}
type Tai struct {
PlmnId *PlmnId `json:"plmnId"`
Tac string `json:"tac"`
Nid string `json:"nid"`
}
type PlmnId struct {
Mcc string `json:"mcc"`
Mnc string `json:"mnc"`
}
The JSON in mongodb is of the form
[
{
"taiList": [
{
"plmnId": {
"mcc": "244",
"mnc": "24"
},
"tac": "00001",
"nid": "99"
},
{
"plmnId": {
"mcc": "244",
"mnc": "34"
},
"tac": "00001",
"nid": "555"
}
]
},
{
"taiList": [
{
"plmnId": {
"mcc": "244",
"mnc": "24"
},
"tac": "00001",
"nid": "99"
}
]
}
]
The JSON "taiList" is formed after the unmarhsal from mongodb, error check intentionally omitted.
func MongoFind(id string) (Data, error) {
filter := bson.M{"_id": id}
var (
result model.Data
)
...
DB.Collection(tailistCollection).FindOne(context.TODO(),filter).Decode(&result)
...
return result, nil
}
The MongoFind returns Data and error. The Data.TaiList array is what i would like to perform the check.
I would like to check if the JSON object "ta" is contain in the "taiList".
var ta = model.Tai{
PlmnId: &model.PlmnId{Mcc: "244", Mnc: "34"},
Tac: "00001",
Nid: "555",
}
I tried with the code below
var data Data
func CheckTai(ta models.Tai, data Data) bool {
for _, Tai := range data.TaiList {
if reflect.DeepEqual(Tai, ta) {
return true
}
}
return false
}
but CheckTai function return false. The CheckTai only return true when there is only one JSON object that match the list as
{
"taiList": [
{
"plmnId": {
"mcc": "244",
"mnc": "24"
},
"tac": "00001",
"nid": "555"
}
]
}
Need help or idea on how to perform this check.