1

This is the data I want to find using device array value 1 for and that object and removes that value from the device array.

find using device array value 1 and remove that value from the array

This is my current object:

[ 
 {
    "devices": [
      "1",
      "2"
    ],
    "eui": "A5AC840871F01D65",
    "key": "5BCBF770D2B43638860F7DA3733ED1B3",
    
  },
  {
    "devices": [
      "3",
      "4"
    ],
    "eui": "A5AC840871F01D65",
    "key": "5BCBF770D2B43638860F7DA3733ED1B3",
    
  }
]

the expected output is :

 {
    "devices": [
      "2"
    ],
    "eui": "A5AC840871F01D65",
    "key": "5BCBF770D2B43638860F7DA3733ED1B3",
    
  },
  {
    "devices": [
      "3",
      "4"
    ],
    "eui": "A5AC840871F01D65",
    "key": "5BCBF770D2B43638860F7DA3733ED1B3",
    
  }
]

1 Answer 1

1

You can use $pull for that. Here:

db.collection.update(
    { devices: "1" },
    { $pull: { devices: "1" } }
)

https://mongoplayground.net/p/ItZwZdtzjO5

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

Comments

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.