Within a MongoDB collection I have a document that contains an object with various KV pairs to describe a particular sensor. The format of the configuration object is shown below. What I want to be able to do is add new array elements into the configuration e.g. rssi, which will themselves contain an array of objects. I will also want to remove objects within the array's. It feels like the $push and $pull operators should used. The query is being coded in golang. sensor_sn can be taken as a unique identifier. Any suggestions on how to accomplish this?
{
"configuration": {
"ta": [
{
"ident": 0,
"description": "Temperature",
"sensor_sn": "828082837284",
"unit": "℃",
"type": "number",
"elemID": "ta0"
},
{
"ident": 1,
"description": "Temperature",
"sensor_sn": "0258c40d0000",
"unit": "℃",
"type": "number",
"elemID": "ta1"
},
{
"ident": 2,
"description": "Temperature",
"sensor_sn": "ed9dc30d0000",
"unit": "℃",
"type": "number",
"elemID": "ta2"
}
],
"rh": [
{
"ident": 0,
"description": "Relative Humidity",
"sensor_sn": "",
"unit": "%",
"type": "number",
"elemID": "rh"
}
],
"vlt2": [
{
"ident": 0,
"description": "Battery Voltage",
"sensor_sn": "",
"unit": "V",
"type": "number",
"elemID": "vlt2"
}
],
"pt": [
{
"ident": 0,
"description": "Precipitation",
"sensor_sn": "",
"unit": "mm/tip",
"type": "number",
"elemID": "pt"
}
]
}
}
I've had a look thru various forums to see if I can find a suitable pattern to modify, but so far without understanding quite how to accomplish my objective. https://www.mongodb.com/docs/manual/reference/operator/update/pull/ MongoDB - Update or Insert object in array