1

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

1 Answer 1

0

Have you looked at $addToSet? It will add a value to an array if it doesn't already exist. $pull will remove a matching item from an array.

For both calls the setup is mostly the same. The way I do it is create a bson.M{} of the new or deleted array element, then create a bson.M{} update using the field name and the update defined above, lastly create the update using the $addToSet or $pull command and what you are setting.

Assuming you are using the MongoDB Go driver, you could try:

    theData := bson.M{"ident": 1, "description": "New item", "sensor_sn": "new sensor", "unit": "V", "type": "number", "elemID": "vlt2"}
    whatToChange := bson.M{"configuration.rssi": theData}

To add the element:
    update := bson.M{"$addToSet": whatToChange}
To delete it:
    update := bson.M{"$pull": whatToChange}

    _, err = theCollection.UpdateOne(
        context.Background(),
        bson.M{"_id": "itemID"},
        update,
    )

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the very quick answer. I did manage to get something similar working with $push, but I shall try the $addToSet operator and see if that is more efficient (for coding)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.