I have the JSON below in mongodb collection
{
"Id":"3fa85f64-5717-4562-b3fc-2c963f66afa6",
"allowedNssaiList": [
{
"allowedSnssaiList": [
{
"allowedSnssai": {
"sst": 1,
"sd": "2"
},
"IMSI": "244340000000001",
"tac": "3022"
}
],
"accessType": "3GPP_ACCESS"
}
]
}
I would like to append to the sub array allowedSnssaiList with the object
{
"allowedSnssai": {
"sst": 1,
"sd": "2"
},
"IMSI": "244340000000001",
"tac": "3022"
}
I have tried with the following
selector := bson.M{"_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6"}
PushToArray := bson.M{"$addToSet": bson.M{"allowedSnssaiList": bson.M{"allowedSnssai": bson.M{"sst": 3,"sd": "4"}, "IMSI": "244510000000004","tac": "3022"}}}
err := db.C(COLLECTION).Update(selector, PushToArray)
but when i push, it does not append well, i get
{
"_id":"3fa85f64-5717-4562-b3fc-2c963f66afa6",
"allowedNssaiList": [
{
"allowedSnssaiList": [
{
"allowedSnssai": {
"sst": 1,
"sd": "2"
},
"IMSI": "244340000000001",
"tac": "3022"
}
],
"accessType": "3GPP_ACCESS"
}
],
"allowedSnssaiList" : [
{
"allowedSnssai" : {
"sst" : 1,
"sd" : "5"
},
"IMSI" : "244340000000005",
"tac" : "3022"
}
]
}
but i want the result or append as
{
"_id":"3fa85f64-5717-4562-b3fc-2c963f66afa6",
"allowedNssaiList": [
{
"allowedSnssaiList": [
{
"allowedSnssai": {
"sst": 1,
"sd": "2"
},
"IMSI": "244340000000001",
"tac": "3022"
},
{
"allowedSnssai" : {
"sst" : 1,
"sd" : "5"
},
"IMSI" : "244340000000005",
"tac" : "3022"
}
],
"accessType": "3GPP_ACCESS"
}
]
}
Even changing to
PushToArray := bson.M{"$addToSet": bson.M{"allowedNssaiList[0].allowedSnssaiList[0]": bson.M{"allowedSnssai": bson.M{"sst": 3,"sd": "4"}, "IMSI": "244510000000004","tac": "3022"}}}
still does not work. Any help about how to achieve my result.
Thanks for the answer below, i was able to append to the right array as wanted however, i would like to modify any of objects appended to the array so i added an extra field ssid as an id for each object as
{
"_id":"3fa85f64-5717-4562-b3fc-2c963f66afa6",
"allowedNssaiList": [
{
"allowedSnssaiList": [
{
"allowedSnssai": {
"sst": 1,
"sd": "2"
},
"IMSI": "244340000000001",
"tac": "3022",
"ssid":1
},
{
"allowedSnssai" : {
"sst" : 1,
"sd" : "5"
},
"IMSI" : "244340000000005",
"tac" : "3022",
"ssid":2
}
],
"accessType": "3GPP_ACCESS"
}
]
}
To modify or update the values for the object with ssid = 2, i have
selector := bson.M{"_id": "3ca85f64-5717-4562-b3fc-2c963f66af33",
"allowedNssaiList.allowedSnssaiList.ssid": 2}
UpdateArray := bson.M{"$set": bson.M{"allowedNssaiList.0.allowedSnssaiList.$": bson.M{"allowedSnssai": bson.M{"sst": 1,
"sd": "1000"}, "IMSI": "244340000000010","tac": "302332", "ssid": "2"}}}
err := db.C(COLLECTION).Update(selector, UpdateArray)
this result in updating the object with ssid = 1 but changes the other object as well to ssid = 1 as below. I would also like to delete a specific object as well using the ssid value. Any help.
{
"ssid" : 1,
"allowedSnssai" : {
"sst" : 1,
"sd" : "1000"
},
"IMSI" : "244340000000010",
"tac" : "302332"
},
{
"allowedSnssai" : {
"sst" : 1,
"sd" : "2"
},
"IMSI" : "244340000000001",
"tac" : "3022",
"zone" : "zone3",
"ssid" : 1
}
Any help as how to achieve this. Is using $set right?