You need to use the $pull
operator, specifying the subcategories
array and the condition used to pick the elements to remove. The $
operator can be used to select the subcategories
array of the specific categories
element you're interested in. See the Update Array Operators docs of mongo for details. Something like the following should work:
val query = MongoDBObject("categories.categoryname" -> "Electronics")
val removingData = $pull("categories.$.subcategories" -> MongoDBObject("subcategoryname" -> "camera"))
collection.update(query, removingData)
If you want to remove multiple subcategories, you can use the $or
operator to specify multiple subcategoryname
s