Answers for "mongodb remove from array"

2

how to delete subdocument array object mongodb

db.getCollection('profilesservices').updateOne({_id: ObjectId("60391a9eefdb7c17bd84c0b1")},
  {
   $pull: { "volunteerExperiences": {_id: ObjectId("60392e7595bb2d87689aca62")} } 
  }
)
Posted by: Guest on February-26-2021
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" ]
}
Posted by: Guest on February-27-2021
4

$pull mongoose

TemplateDoc.findOneAndUpdate(
    { userId: _id },
    { $pull: { templates: { _id: templateid } } },
    { new: true }
  )
    .then(templates => console.log(templates))
    .catch(err => console.log(err));
Posted by: Guest on May-01-2020
0

how to remove an array element by its index in mongodb

> db.removeArrayElementByItsIndexDemo.update({}, {$unset : {"InstructorSubject.2" : 1 }});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Posted by: Guest on August-27-2021
-1

Mongodb Remove array element by index

db.example.update({}, [
     {$set: {field: {
           $concatArrays: [ 
                  {$slice: ["$field", P]}, 
                  {$slice: ["$field", {$add: [1, P]}, {$size: "$field"}]}
           ]
     }}}
]);
Posted by: Guest on May-04-2021

Code answers related to "mongodb remove from array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language