Reputation: 3370
How can i replace all elements from a mongodb array element. As in below example, my requirement is to remove both object from skills array and add new elements in it.
{
"_id": "uniqueid",
"skills": [
{ "skill": "dancer" },
{ "skill": "singer" }
]
}
I need to replace all element of skills array field. How this can be achieved using mongodb java driver, or other query types?
Upvotes: 0
Views: 1218
Reputation: 4010
You just need to use $set operator $set to set skills to a new value which will be an array of the new element you want to replace with.
db.collectionName.update(
{ _id: 'uniqueid' },
{ $set:
{
skills: [{'new elements'}]
}
}
)
if you want to remove all elements set the skills to an empty array {skills: []}
if you want to remove a certain elements based on a value use $pull operator $pull
Upvotes: 2