Doc:
{
_id: 5150a1199fac0e6910000002,
name: \'some name,
items: [{
id: 23,
name: \'item name 23\'
},{
id: 24,
name: \'ite
my database:->
{
"_id" : ObjectId("5806056dce046557874d3ab18"),
"data" : [
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
}
]
}
MY QUERY:->
db.getCollection('play_table').update({},{$pull:{"data":{"id":3}}},{multi:true}
OutPut:->
{
"_id" : ObjectId("5806056dce046557874d3ab18"),
"data" : [
{
"id" : 1
},
{
"id" : 2
}
]
}
I have a document like
I have to delete address from address array
After searching lots on internet I found the solution
Customer.findOneAndUpdate(query, {$pull: {address: addressId}}, function(err, data){
if(err) {
return res.status(500).json({'error' : 'error in deleting address'});
}
res.json(data);
});
You can try it also:
db.getCollection('docs').update({ },{'$pull':{ 'items':{'id': 3 }}},{multi:true})
Use $pull
to remove the data
return this.mobiledashboardModel
.update({"_id": args.dashboardId}, { $pull: {"viewData": { "_id": widgetId}}})
.exec()
.then(dashboardDoc => {
return {
result: dashboardDoc
}
});
try..
db.mycollection.update(
{'_id': ObjectId("5150a1199fac0e6910000002")},
{ $pull: { "items" : { id: 23 } } },
false,
true
);
Kishore Diyyana:
If you want to remove all elements including key of the element attributes list. Here is the example of mongoDB unset operator:
db.UM_PREAUTH_CASE.update( { 'Id' : 123}, { $unset: { dataElements: ""} } )
JSON Look like this:
{ "Id":123,"dataElements" : [ { "createdBy" : "Kishore Babu Diyyana", "createdByUserId" : 2020 }, { "createdBy" : "Diyyana Kishore", "createdByUserId" : 2021 } ] }