2

I have a MongoDB embedded document where it has many documents in arrays.

EX:

{
    "_id" : ObjectId("594b7b7d4064e264420e38d0"),
    "UnitId" : "594b75d44064e264420e3869",
    "UnitName" : "PARKING",
    "UnitIcon" : "./upload/file-1498117588607.jpg", 
    "Stream" : {
        "_id" : ObjectId("594b7b7d4064e264420e38d1"),
        "Types" : [ 
            {
                "TypeId" : "594b9a884064e264420e3b56",
                "TypeName" : "TR/RC",
                "Travel" : false
            }, 
            {
                "TypeId" : "594b9a844064e264420e3b55",
                "MaterialStreamName" : "RW",              
                "Travel_Required" : false
            }, 
            {
                "TypeId" : "594b9a9d4064e264420e3b58",
                "TypeName" : "ST",               
                "Travel_Required" : true
            }, 
            {
                "TypeId" : "594b9a764064e264420e3b53",
                "TypeName" : "FD",               
                "Travel_Required" : true
            }
        ]
    }   
},{
    "_id" : ObjectId("594b7b7d4064e264420e38d1"),
    "UnitId" : "594b75d44064e264420e3870",
    "UnitName" : "CAFE",
    "UnitIcon" : "./upload/file-1498117588608.jpg", 
    "Stream" : {
        "_id" : ObjectId("594b7b7d4064e264420e38d2"),
        "Types" : [ 
            {
                "TypeId" : "594b9a884064e264420e3b56",
                "TypeName" : "TR/RC",
                "Travel" : false
            }, 
            {
                "TypeId" : "594b9a844064e264420e3b55",
                "MaterialStreamName" : "RW",              
                "Travel_Required" : false
            }, 
            {
                "TypeId" : "594b9a9d4064e264420e3b58",
                "TypeName" : "ST",               
                "Travel_Required" : true
            }, 
            {
                "TypeId" : "594b9a764064e264420e3b53",
                "TypeName" : "FD",               
                "Travel_Required" : true
            }
        ]
    }   
},{
    "_id" : ObjectId("594b7b7d4064e264420e38d2"),
    "UnitId" : "594b75d44064e264420e3870",
    "UnitName" : "CAFE",
    "UnitIcon" : "./upload/file-1498117588608.jpg", 
    "Stream" : {
        "_id" : ObjectId("594b7b7d4064e264420e38d3"),
        "Types" : [                 
            {
                "TypeId" : "594b9a9d4064e264420e3b58",
                "TypeName" : "ST",               
                "Travel_Required" : true
            },     
            {
                    "TypeId" : "594b9a884064e264420e3b56",
                    "TypeName" : "TR/RC",
                    "Travel" : false
                }, 
                {
                    "TypeId" : "594b9a844064e264420e3b55",
                    "MaterialStreamName" : "RW",              
                    "Travel_Required" : false
                }, 
                {
                    "TypeId" : "594b9a764064e264420e3b53",
                    "TypeName" : "FD",               
                    "Travel_Required" : true
                }
            ]
        }   
    }

I have many documents with the combination of UnitID and TypeID (inside Types object). I have to delete that Type element by match UnitId and TypeID. I have used $pull but didn't achieve.

    db.getCollection('units_details').update(
    { "UnitId" : "594b75d44064e264420e3870","Stream[0].Types.$.TypeID" : "594b9a884064e264420e3b56"    }, 
    { $pull: { "Stream[0].Types"            
                 "TypeId" : "594b9a884064e264420e3b56",
                        "TypeName" : "TR/RC",                            
                        "Travel" : false
          } }} ,{multi:true}

    );

How to achieve the delete the element inside that types array where that Type order in Types array is changing.

charan tej
  • 1,054
  • 10
  • 29
  • Do you've the correct document ? I don't see `Material_Stream_Flow_Details` – s7vr Feb 23 '18 at 14:29
  • @Veeram edited please check now – charan tej Feb 23 '18 at 14:37
  • Something like `db.getCollection('units_details').update( { "UnitId" : "594b75d44064e264420e3870"}, { $pull: { "Stream.Types" :{ "TypeId" : "594b9a884064e264420e3b56", "TypeName" : "TR/RC", "Travel" : false } } },{multi:true} );` should work – s7vr Feb 23 '18 at 14:40
  • @Veeram stream also an array have some elements. Types is in [0] position – charan tej Feb 23 '18 at 14:43
  • Please update your documents. It shows stream as embedded doc – s7vr Feb 23 '18 at 14:44
  • @Veeram yes it is, but always types is in first position[0]. that's why I didn't mention others. but Inside Types element positions will change like "TR/RC" comes [0] in one document and [2] in another document – charan tej Feb 23 '18 at 14:47

1 Answers1

1

You can use either of below queries to query the nested arrays.

db.getCollection('units_details').update(
  {"UnitId":"594b75d44064e264420e3870","Stream.Types.TypeId":"594b9a884064e264420e3b56"},
  {"$pull":{
    "Stream.$.Types":{
      "TypeId":"594b9a884064e264420e3b56",
      "TypeName":"TR/RC",
      "Travel":false}
  }},
  {"multi":true}
);

OR ( More applicable for multi criteria scenario )

db.getCollection('units_details').update( {
    "UnitId":"594b75d44064e264420e3870",
    "Stream":{
      "$elemMatch":{
        "Types":{
          "$elemMatch":{"TypeId":"594b9a884064e264420e3b56"}
        }
      }
    }
  },
  {"$pull":{
    "Stream.$.Types":{
      "TypeId":"594b9a884064e264420e3b56",
      "TypeName":"TR/RC",
      "Travel":false}
  }},
  {"multi":true}
 )
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • Thanks for the Quick help. The second Query is working but the first query is not working and I suspect it is due to the position of Types in condition `Stream.Types.TypeId`. – charan tej Feb 23 '18 at 15:20
  • can you please help on this https://stackoverflow.com/q/51474838/5756149 – charan tej Jul 23 '18 at 14:55