mongodb delete from array code example

Example 1: how to delete subdocument array object mongodb

db.getCollection('profilesservices').updateOne({_id: ObjectId("60391a9eefdb7c17bd84c0b1")},
  {
   $pull: { "volunteerExperiences": {_id: ObjectId("60392e7595bb2d87689aca62")} } 
  }
)

Example 2: mongodb remove from array

// query
db.stores.update(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } },
    { multi: true }
)

// sample data
{
   _id: 1,
   fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
   vegetables: [ "carrots", "celery", "squash", "carrots" ]
}
{
   _id: 2,
   fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
   vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}